【发布时间】:2017-07-01 15:42:34
【问题描述】:
我需要一些帮助。 我有一个控制器,其中包含一个名为“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();
});
});
但是当它们嵌套时,第二个自定义指令只是在父指令的范围内查看。知道如何通过函数调用吗?
【问题讨论】:
-
我很惊讶嵌套的自定义指令可以调用
scope.loadInformation()并在父指令中找到它。两个指令都具有隔离作用域——它们根本不应该看到它们的父作用域。
标签: javascript angularjs