我研究这个问题已经有好几个小时了,最后我在plunker上转载了它。
当使用外部资源作为模板的自定义指令与ng-repeat结合使用时,在更改模型时视图无法正确呈现。
在我的示例中,单击链接将替换模型,但旧数据尚未清除。
如果我使用template: 'stringTemplate'
代替templateUrl: 'urlToTemplate'
,它就可以正常工作。还是不知道这是个bug还是什么...
angular.module('test', [])
.run(function($rootScope) {
$rootScope.topics = [{
content: 'Click here to change reply',
replys: [{
content: 'Reply test...',
}]
}];
})
.directive('topic', function() {
return {
replace: true,
restrict: 'E',
templateUrl: 'topic.htm',
link: function(scope) {
scope.reply = function(input) {
scope.topic.replys = [{ content: '"Reply test..." should be replaced, but it\'s not!' }];
}
}
};
})
.directive('reply', function() {
return {
replace: true,
restrict: 'E',
// template: '<div><div ng-bind="reply.content"></div></div>' //this works fine
templateUrl: 'reply.htm' // same content
};
});
我做了一些研究,看来您并不孤单:
https://github.com/angular/angular.js/issues/2151
ishw用户提到了一个快速解决方案:
“对于那些可能尚未意识到的人:这是因为您的ng-repeat位于指令模板的根元素上。将ng-repeat包裹在任何元素中就可以了。”
<div>
<div class="topic" ng-bind="topic.content" ng-click="reply()"></div>
<div ng-repeat="reply in topic.replys"><reply></reply></div>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句