我有一个 AngularJS 应用程序,它依赖于 Web 服务,我想在加载应用程序后从远程主机将更多 Controller 加载到应用程序中。我不知道这可能吗?
在我的 Controller 中,我想加载更多 Controller (js 文件)
.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 内置对象(指令、 Controller 、工厂等)。
这意味着您的 Controller (来自 Web 服务)应该加载到引导 Angular 上(可能作为索引页面上的资源)。
有一些方法可以在引导后动态加载内容,以下是一些:
- 我个人最喜欢的: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/
还有很多东西可以找到..你显然可以用谷歌搜索它。
关于AngularJS加载webservice提供的远程JS Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25401271/