2016-07-13 145 views
-1

我有與來自另一個和代碼指令訪問的一個問題是低於如何從別人angularjs訪問指令?

app.module('myApp', []) 
.directive('vikiQues', ['$http', '$compile', function($http, $compile){ 
return { 
    restrict : 'E', 
    scope : true, 
    controller : [ 
     function(){ 
     } 
    ], 
    link : function(scope, iElement, iAttrs){ 
     $http.get('getOutside/1') 
      .then(function(data){ 
       iElement.html($compile($(data.data))(scope)); 
       /*scope.addThere.find('div.please-wait').remove(); 
       scope.questionList.push({ 
        text : '', 
        options : [] 
       });*/ 
      }); 
    } 
}; 
}]).directive('vikiOption', ['$http', '$compile', function ($http, $compile) { 
return { 
    restrict: 'E', 
    scope : true, 
    require : '^vikiQues', 
    link: function (scope, iElement, iAttrs, vikiQuesCtrl) { 
     $http.get('getInside/1') 
      .then(function(data){ 
       var _ = $(data.data); 
       scope._opt = false; 
       iElement.html($compile(_)(scope)); 
       if (scope.$parent.questionList[scope.$parent.totalQuestionCount-1].options.length > 2) 
        _opt = true; 
       scope.now = { 
        id : scope.$parent.questionList[scope.$parent.totalQuestionCount-1].options.length, 
        char : '', 
        text : '', 
        image : '', 
        removeable : scope._opt, 
       }; 
       ques.now.options.push(scope.now); 
      }); 
    } 
}; 

}]);

我得到這個錯誤每次:angular.min.js:117錯誤:[$編譯:ctreq] http://errors.angularjs.org/1.5.7/ $編譯/ ctreq P0 = vikiQues & P1 = vikiOption

什麼有錯?

HTML代碼:
0" >
viki-ques和viki-option是來自php的模板。 如果刪除 iElement.html($編譯($(data.data))(範圍)); 代碼,我不能得到錯誤。我想我有錯誤。 ithink $ compile或scope提供這個錯誤。(幫助)

+0

安置自己的HTML。 – dfsq

+0

我從PHP的問題模板和組HTML到VIKI,疑問句元素,並得到再次PHP一些選項,並設置HTML來VIKI選項... 是足夠? – maviofis

+1

沒有足夠的信息來幫助。用這個問題創建演示,然後很容易找出問題。 – dfsq

回答

0

你可以嘗試

require : 'vikiQues', 

或你應該改變

controller : [ 
     function(){ 
     } 
    ], 

到:

controller : function(){}, 
+0

謝謝你,但得到了相同的錯誤 – maviofis

+0

嘗試第二個 – daymosik

+0

謝謝,但不工作。請幫忙 – maviofis

0

根據directives documentation(檢索算法爲創建指令進行通信)你需要你的父指令li柯本:

.directive('vikiOption', function(){ 
    return { 
     restrict : 'E', 
     require: '^vikiQues', 
     template: '<li>my items</li>', 
     //templateUrl: 'url to html file with html from previous line', 
     scope : true, 
     link : function (scope, elem, attrs, ctrl){ console.log(ctrl); } 
    }; 
}); 

字符^意味着角將嘗試上找到所需的指令在標記層次

,你需要有一個控制器在你vikiQues指令,聲明它像這樣

.directive('vikiQues', function(){ 
    return { 
     restrict : 'E', 
     transclude: true, //attention here!! 
     scope : true, 
     template: '<ul ng-transclude>item x</ul>', //and here, now you can instert vikiOption inside 
     controller : function(){ //pay attention no brackets 
     this.myProp = 'foo'; 
     } 
    }; 
}); 

和您的標記使用此:

<viki-queues> 
    <viki-option>can use ng-repet for viki-option<viki-option/> 
    <viki-option>to display a list of them<viki-option/> 
</viki-queues> 

P.S. templateUrl更方便,因爲你可以保持您的標記在單獨的文件或類似asp.net或其他或只是他們的服務器模板引擎把它作爲普通的靜態HTML文件

P.P.S希望這有助於

+0

所以你只是重複了OP已經有的:'^ vikiQues'和控制器都在那裏。 – dfsq

+0

我從來沒有見過陣列中的控制器聲明,所以它不是一樣的 – Denis

+0

我看到這個聲明無處不在,所以我使用它但不是 – maviofis