2016-11-28 146 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();