2016-11-28 141 views
1

我使用$("#div1").load("aa.html")将“aa.html”加载到网页中,在“aa.html”中有一个“ng-repeat”指令,加载后“ng-repeat”wasn没有被angularjs执行,它直接出现在DOM中,但是如果我直接在$(“#div1”)中放置“aa.html”内容,它可以由angularjs执行。我如何让jquery加载这个页面后,angularjs执行这个“ng-repeat”?异步执行“ng-repeat”

+0

我不能使用ng-include,因为它需要确定是否加载它的逻辑。 –

回答

2

您需要注册contorller

function registerController(moduleName, controllerName, template, container) { 
// Load html file with content that uses Ctrl controller 
//$(template).appendTo(container); 
// Here I cannot get the controller function directly so I 
// need to loop through the module's _invokeQueue to get it 
var queue = angular.module(moduleName)._invokeQueue; 
for (var i = 0; i < queue.length; i++) { 
    var call = queue[i]; 
    if (call[0] == "$controllerProvider" && 
     call[1] == "register" && 
     call[2][0] == controllerName) { 
     controllerProvider.register(controllerName, call[2][1]); 
    } 
} 

angular.injector(['ng', moduleName]).invoke(function ($compile, $rootScope) { 
    $compile($("#" + template))($rootScope); 
    $rootScope.$apply(); 
}); 

}后

在你的jQuery成功

registerController( '应用程序名称', '控制器', “temolatename”)

angular.element("element").scope().run(); 
    angular.element("element").scope().$apply();