Ich habe einen Bottom geschrieben, um zu sehen, wie bindToDirective verwendet wird, um Bereiche zu isolieren und den Direktiven-Controller zum Aufruf der Hauptcontrollerfunktion zu verwenden, aber ich mache etwas falsch. Könnten Sie vorschlagen?Aufruf der Controller-Funktion aus der Anweisung mithilfe von bindToController
Dies ist Plunker: http://plnkr.co/edit/UJLjTmIiHydHr8qRzAsX?p=preview
Codebeispiel:
.controller('Ctrl', function() {
var self = this;
self.func = function() {
console.log('In the controller function');
};
})
.directive('myDirective', [ function() {
var self = {};
self.link = function (scope, elem, attrs, ctrl) {
elem.bind('click', function() {
ctrl.ctrlFunc();
});
elem.addClass('fa fa-file-excel-o fa-lg');
};
return {
restrict: 'E',
scope: {},
controller: function() {
},
controllerAs: 'DirCtrl',
bindToController: {
ctrlFunc: '&'
},
link: self.link
};
}])
html Probe Haupt-Controller-Funktion zuordnen Direktive:
<div ng-controller="Ctrl">
<my-directive ctrlfunc="Ctrl.func()"></my-directive>
</div>