【发布时间】:2014-12-17 17:58:29
【问题描述】:
AngularJS 的新手,我很难理解指令的一些细节。我有一个场景,我想使用不包含嵌入的指令(我认为?),使用指令的隔离控制器内的指令的属性值来获取额外的数据以提供给指令的模板......(希望满嘴的话对吗?)。
也许这个伪示例会让我更清楚我想要实现的目标:
someDirective.js:
angular.module('someDirective', [
'someService',
])
.directive('someDirective', function() {
function SomeController($scope, someService) {
$scope.additionalData = someService.getStuff(/* ??? I want to use 'someKey' here */);
}
return {
restrict: 'E',
controller: SomeController,
scope: {
someKey: '='
},
template: '{{additionalData | json}}'
};
});
directiveUser.tpl.html:
<someDirective someKey="someKeyFromTheOuterScope"/>
请注意,我的指令模板不需要直接使用键值,而是使用指令控制器提供的附加数据。
我读过的所有文档要么直接在模板中使用提供的范围属性数据,要么通过指令链接函数的“attrs”参数。我不认为链接是我想要的,因为我不是在操作 DOM,而只是想为指令的模板提供额外的数据。
我在这里从根本上误解了什么?
【问题讨论】: