AngularJS load remote JS controller provided by webservice

杀马特。学长 韩版系。学妹 提交于 2019-12-22 13:58:17

问题


I have a AngularJS app, that depends on a webservice, I would like to load some more controllers into the app from a remote host, after the app is loaded. I don't know i this is possible?

In my controller, I want to load some more controllers (js files)

.controller('FrontpageCtrl', function($scope, $stateParams, $filter, $sce, contentService) {
    console.log("hitting FrontpageCtrl ... ");
    contentService.promise.then(function(data){
        var page = $filter('filter')(data, {id:$stateParams.pageId})[0];
        $scope.content = $sce.trustAsHtml(page.content);

        // Load a controller, directive and other provided by the webservice!
        $scope. ...
    });
})

回答1:


Currently angular does not provide a way to load modules dynamically. Hence, any angular built in object (directives, controllers, factories, etc.).
This means your controllers (from the web service) should be loaded on bootsrapping angular (probably as a resource on the index page).
There are some ways to dynamically load stuff after bootstrapping, here are a few:

  1. My personal favorite: https://github.com/ocombe/ocLazyLoad.
  2. Closest to your question: http://weblogs.asp.net/dwahlin/dynamically-loading-controllers-and-views-with-angularjs-and-requirejs
  3. https://www.startersquad.com/blog/angularjs-requirejs/

There are many more stuff to be found.. you can obviously google it.



来源:https://stackoverflow.com/questions/25401271/angularjs-load-remote-js-controller-provided-by-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!