通过Web服务提供AngularJS负荷遥控器JS遥控器、负荷、Web、AngularJS

2023-09-14 23:08:12 作者:別再為他流淚

我有一个AngularJS应用,依赖于Web服务,我想更多的控制器加载到应用程序从远程主机,应用程序加载后。我不知道我这是可能的吗?

在我的控制器,我想加载一些更多的控制器(js文件)

  .controller('FrontpageCtrl',函数($范围,$ stateParams,$过滤器,$ SCE,contentService的){    的console.log(打黑FrontpageCtrl ......);    contentService.promise.then(功能(数据){        VAR页= $过滤器('过滤器')(资料,{ID:$ stateParams.pageId})[0];        $ scope.content = $ sce.trustAsHtml(page.content);        //装载由Web服务提供了一个控制器,指令等!        $范围。 ...    });}) 

解决方案 AngularJS 中文版电子书 Web开发文档类资源 CSDN下载

目前的角度不提供一种方式来动态加载的模块。因此,中建对象(指令,​​控制器,工厂等)的任何角度。这意味着你的控制器(从Web服务)应在bootsrapping角(可能是因为索引页上的资源)被加载。有一些方法为动态加载引导后的东西,这里有几个:

我个人最喜欢的: https://github.com/ocombe/ocLazyLoad 。最近你的问题:http://weblogs.asp.net/dwahlin/dynamically-loading-controllers-and-views-with-angularjs-and-requirejs https://www.startersquad.com/blog/angularjs-requirejs/

有很多更多的东西被发现..你可以明显google一下。

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. ...
    });
})

解决方案

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:

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

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