2016-07-13 15 views
-1

ich habe ein Problem mit der Richtlinie den Zugriff von einem anderen und Code unterWie kann auf die Direktive von anderen in angularjs zugegriffen werden?

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); 
      }); 
    } 
}; 

}]);

Ich habe diesen Fehler jedes Mal: ​​angular.min.js: 117 Fehler: [$ Kompilierung: ctreq] http://errors.angularjs.org/1.5.7/ $ kompilieren/ctreq p0 = vikiQues & p1 = vikiOption

, was da drin ist falsch?

HTML-Code:
0" >
viki-ques und viki-option sind Vorlagen, die von php kommen. wenn ich entferne iElement.html ($ compile ($ (data.data)) (Bereich)); code, ich kann keinen fehler bekommen. Ich denke, dass ich dort Fehler habe. ithink $ compile oder scope liefern diesen Fehler.(Hilfe)

+0

Post HTML erhalten. – dfsq

+0

i eine Frage Vorlage von php bekommen und html Viki-ques Element und bekommen etwas Option von php wieder und setzen html Viki-Option ... ist es gesetzt genug? – maviofis

+1

Nicht genügend Informationen, um zu helfen. Erstellen Sie eine Demo mit dem Problem, dann ist es einfach, das Problem herauszufinden. – dfsq

Antwort

0

Sie

require : 'vikiQues', 

versuchen könnte, oder Sie sollten

controller : [ 
     function(){ 
     } 
    ], 

ändern:

controller : function(){}, 
+0

danke für aber habe gleichen Fehler – maviofis

+0

versuchen Sie die zweite – daymosik

+0

danke, aber nicht funktionieren. Bitte helfen Sie – maviofis

0

nach directives documentation (serach für Erstellen von Richtlinien, die Kommunizieren) Sie müssen Ihre Elternrichtlinie verlangen ke dies:

.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); } 
    }; 
}); 

Charakter ^ bedeutet Winkel werden versuchen oberen

in Markup-Hierarchie erforderlich Richtlinie zu finden, und Sie müssen einen Controller in Ihrer vikiQues Richtlinie haben, erklären es so

.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'; 
     } 
    }; 
}); 

und Ihr Markup verwenden dies:

<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 ist praktisch, da Sie Ihr Markup in separaten Datei zu halten oder es von Template-Engine wie asp.net oder andere oder nur Server sie als gemeinsame statische HTML-Dateien

P.P.S hoffe, das hilft

+0

Sie haben also einfach wiederholt, was OP bereits hat: Sowohl 'vikiQues' als auch Controller sind da. – dfsq

+0

Ich habe nie Controller Deklaration in Array gesehen, so ist es nicht das gleiche – Denis

+0

Ich sah diese Erklärung überall, so dass ich es verwenden, aber nicht – maviofis