指令:
angular.module('myApp')
.directive('rankingTags', function() {
return {
restrict: 'E',
scope: {},
controller: function($scope) {
$scope.updateTagList = function(tag) {
console.log('updateTagList function called') // never logged
};
},
templateUrl: '/app/common/directives/tags.tpl.html',
replace: true
};
});
指令的模板:
<div class="tag" ng-repeat="tag in $root.collection.tags track by $index">
<input ng-click="updateTagList(tag)" type="checkbox" />
<label>{{tag.name}}</label>
</div>
从另一个模板调用指令:
<ranking-tags></ranking-tags>
我无法从指令模板中访问
updateTagList
函数。因此,这个控制器与模板没有链接起来。请问我做错了什么?
'NodeJs'
要用引号括起来?不能进行双向绑定到一个字符串。意图是什么?此外,当标签集合已经传递到指令中时,为什么repeat
还要寻找$root
? - charlietfltags
列表为空。updateTagList
函数会更新它,然后您可以在页面控制器中使用$watch
捕获更改。另一方面,$root.collection.tags
($rootScope 值)具有标签列表。 - MichaelupdateTagList
不可用。如果您需要进一步的帮助,请提供一个MCVE。把这些内容放到plunker中展示给我们看有多难? - charlietfl