Javascript 带有templateUrl和ng repeat的自定义指令
我已经研究这个问题好几个小时了,最后 这是我的问题: 当使用外部资源作为模板的自定义指令与ng repeat组合时,模型更改时视图无法正确呈现 在我的示例中,单击链接将替换模型,但旧数据尚未清理 如果我使用Javascript 带有templateUrl和ng repeat的自定义指令,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我已经研究这个问题好几个小时了,最后 这是我的问题: 当使用外部资源作为模板的自定义指令与ng repeat组合时,模型更改时视图无法正确呈现 在我的示例中,单击链接将替换模型,但旧数据尚未清理 如果我使用template:'stringTemplate'而不是templateUrl:'urlToTemplate',它就可以正常工作。 还是不知道是虫子还是什么 部分代码: 角度模块('test',[]) .run(函数($rootScope){ $rootScope.topics=[{ 内容:“
template:'stringTemplate'
而不是templateUrl:'urlToTemplate'
,它就可以正常工作。
还是不知道是虫子还是什么
部分代码:
角度模块('test',[])
.run(函数($rootScope){
$rootScope.topics=[{
内容:“单击此处更改答复”,
答复:[{
内容:“回复测试…”,
}]
}];
})
.directive('topic',function(){
返回{
替换:正确,
限制:'E',
templateUrl:'topic.htm',
链接:功能(范围){
scope.reply=函数(输入){
scope.topic.replys=[{content:''Reply test…“应该被替换,但它不是!”}];
}
}
};
})
.directive('reply',function(){
返回{
替换:正确,
限制:'E',
//模板:“”//这很好用
templateUrl:'reply.htm'//相同的内容
};
});
我做了一些研究,在这个问题上,似乎不止你一个人: 用户ishw提到,作为一个快速修复: “对于那些可能还没有意识到这一点的人:这是因为您的ng repeat位于指令模板的根元素上。将您的ng repeat包装在任何元素中都可以。” :
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
};
});
<div>
<div class="topic" ng-bind="topic.content" ng-click="reply()"></div>
<div ng-repeat="reply in topic.replys"><reply></reply></div>
</div>