AngularJS 加载 webservice 提供的远程 JS 控制器 [英] AngularJS load remote JS controller provided by webservice

查看:16
本文介绍了AngularJS 加载 webservice 提供的远程 JS 控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 AngularJS 应用程序,它依赖于网络服务,我想在应用程序加载后从远程主机将更多控制器加载到应用程序中.我不知道这可能吗?

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?

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

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

推荐答案

目前 angular 不提供动态加载模块的方法.因此,任何 angular 内置对象(指令、控制器、工厂等).
这意味着您的控制器(来自 Web 服务)应该在 bootsrapping angular 上加载(可能作为索引页面上的资源).
有一些方法可以在引导后动态加载内容,这里有一些:

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. 我个人的最爱:https://github.com/ocombe/ocLazyLoad.
  2. 最接近您的问题: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.

这篇关于AngularJS 加载 webservice 提供的远程 JS 控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆