[英]Angular call controller function from nested custom directive
我可以帮忙。 我有一个包含名为“ loadInformation()”的函数的控制器。 在此控制器内部,我使用的服务正在使用自定义指令进行一些DOM操纵。 这些是指令:
第一个自定义指令:
angular.module('...').directive('ngInputString', function () {
return {
restrict: 'AE',
replace: 'true',
scope: {
savedInformation: '=',
type: '@',
values: '='
},
templateUrl: 'directives/templates/inputString.html',
link: function (scope, element, attrs) {
scope.filterOb = {ttype: scope.type};
}
}
});
HTML档案:
<div>
<input ttype="{{type}}" type="text" placeholder="{{param.name}}" value='{{param.value}}'
class='clean-input form-control'/>
<ng-saved-information type="STRING" saved-information="savedInformation"></ng-saved-information>
</div>
嵌套的自定义指令:
angular.module('semtrosApp').directive('ngSavedInformation', function () {
return {
restrict: 'AE',
replace: 'true',
scope: {
savedInformation: '=',
type: '@'
},
template: '<ul><li ng-repeat="information in savedInformation | filter:filterOb">{{information.value}}<button type="button" ng-click="..?..">Use Information</button></li></ul>',
link: function (scope, elem, attrs) {
scope.filterOb = {ttype: scope.type};
}
}
});
当我不使用嵌套指令时,它与那段代码配合就可以了:
elem.bind('click', function() {
scope.$apply(function() {
scope.loadInformation();
});
});
但是,当它们嵌套时,第二个自定义指令只是在父指令的范围内查找。 知道如何通过函数调用吗?
看起来ngInputString
指令已经接收了一些数据,并将其传递给ngSavedInformation
。 为什么还不接受loadInformation
回调并将其传递下去呢?
angular.module('...').directive('ngInputString', function () {
return {
scope: {
savedInformation: '=',
type: '@',
values: '=',
loadInformation: '&'
},
// etc
}
});
<ng-saved-information type="STRING" saved-information="savedInformation" load-information="loadInformation()"></ng-saved-information>
angular.module('semtrosApp').directive('ngSavedInformation', function () {
return {
scope: {
savedInformation: '=',
type: '@',
loadInformation: '&'
},
// etc
}
});
另外,您可以在模板中执行以下操作,而不是手动创建点击处理程序:
ng-click="loadInformation()"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.