AngularJS - 将对象传递给ng-include(动态)

时间:2013-11-10 11:19:36

标签: javascript angularjs object

下面我有一个工作示例......但有一个问题... html中的node.title工作正常...但是当{{node.title}}进入ng-include文件时...不起作用。只有全局$ scope是可见的,而不是“node”对象

var element = angular.element($(".withOptions"));
var scope = element.scope();
var injector = element.injector();
var compile = injector.get('$compile');


compile(
       '<li id="list_77"><div><i class="icon20 i-folder-open"></i>'+node.title+' <i class="icon16 i-arrow-down-2"></i><div ng-include="\'http://localhost/test/public/theme\'"></div></div></li>'
       )(scope).appendTo($(".withOptions"));

1 个答案:

答案 0 :(得分:0)

当您致电时,您的代码会抛出异常:

compile(
       '<li id="list_77"><div><i class="icon20 i-folder-open"></i>'+node.title+' <i class="icon16 i-arrow-down-2"></i><div ng-include="\'http://localhost/test/public/theme\'"></div></div></li>'
       )(scope).appendTo($(".withOptions"));

因为您致电appendTo

时没有compile("yourtemplate")(scope)方法

你应该尝试:

compile(
           '<li id="list_77"><div><i class="icon20 i-folder-open"></i>'+node.title+' <i class="icon16 i-arrow-down-2"></i><div ng-include="\'http://localhost/test/public/theme\'"></div></div></li>'
           )(scope,function(clonedElement,scope){
                clonedElement.appendTo($(".withOptions"));
          });