【问题标题】:Execute "ng-repeat" asynchronously异步执行“ng-repeat”
【发布时间】:2016-11-28 07:12:56
【问题描述】:

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

【问题讨论】:

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

标签: jquery angularjs ng-repeat


【解决方案1】:

您需要注册控制器

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('appname', 'controller', "temolatename")

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 2015-12-06
    • 1970-01-01
    • 2017-12-22
    • 2014-12-11
    相关资源
    最近更新 更多