AngularJS指令动态模型绑定到视图
我有以下指示:AngularJS指令动态模型绑定到视图,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我有以下指示: app.directive('renderPartial', function($compile) { return { restrict: "AE", link: function(scope, element, attrs) { var path = getPartial(attrs.module, attrs.file); //path = /abc/some_file.html scope
app.directive('renderPartial', function($compile) {
return {
restrict: "AE",
link: function(scope, element, attrs) {
var path = getPartial(attrs.module, attrs.file);
//path = /abc/some_file.html
scope[attrs.model] = path;
var el = $compile('<div ng-include="attrs.model"></div>')(scope);
element.html(el);
}
}
});
app.directive('renderPartial',函数($compile){
返回{
限制:“AE”,
链接:函数(范围、元素、属性){
var path=getPartial(attrs.module,attrs.file);
//path=/abc/some_file.html
范围[attrs.model]=路径;
变量el=$compile(“”)(范围);
html(el);
}
}
});
我认为:
<render-partial module="abc" file="some_file" model="some_model"></render-partial>
现在由于某种原因,这不起作用,没有错误。但文件未被渲染
我的问题:更新 根据评论中所附的plunker:- 你需要做两件事 1) 使用
var el=$compile(“”)(范围)代码>
2) element.append(el)代码>而不是平面HTML:-P
您可以将其动态化,如下所示:
myApp.directive('renderPartial', function($compile) {
return {
restrict: "AE",
link: function(scope, element, attrs) {
var path = 'test.html';
scope.path = path;
var el = $compile('<div ng-include="path"></div>')(scope);
element.append(el);
}
}
});
myApp.directive('renderPartial',function($compile){
返回{
限制:“AE”,
链接:函数(范围、元素、属性){
var path='test.html';
scope.path=路径;
变量el=$compile(“”)(范围);
元素。追加(el);
}
}
});
不,我希望它能够工作:var el=$compile(“”)(范围)代码>我的意思是,在ng include中只放置attrs.model的值,而不包含scop,因为它是一个粘合的b/w视图和逻辑。因此,假设attr.model有“one”,那么scope[attrs.model]将变为scope[one],它与scope.one的值相等,并且在视图中它将只有“one”。你不需要在它前面加上scope这个词代码>仍然不是working@ntechi你能在fiddle或plunker中复制它吗?很好的快乐编码:)如果我有多个不同文件的相同指令调用,这将不起作用,因为作用域不是数组类型检查我的更新答案:-P