AngularJS指令动态模型绑定到视图

时间:2015-03-25 09:18:23

标签: 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[attrs.model] = path;
         var el = $compile('<div ng-include="attrs.model"></div>')(scope);
         element.html(el);
      }
   }
});

在我看来:

<render-partial module="abc" file="some_file" model="some_model"></render-partial>

现在由于某种原因,这不起作用,没有错误。但是文件没有被渲染。

我的问题:http://plnkr.co/edit/CkTE2pV4i5LvL60NEYfE

2 个答案:

答案 0 :(得分:1)

<强>更新

根据评论中附带的plunker: -

你需要做几件事

1)使用var el = $compile('<div ng-include="'+attrs.model+'"></div>')(scope);

2)element.append(el);而不是平面HTML :-P

Plunker

答案 1 :(得分:0)

你可以让它变得动态,如下:

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);
      }
   }
});

Fiddle