如何动态加载我的AngularJS应用程序依赖项

时间:2014-10-28 20:55:30

标签: angularjs

让我们说,我有像这样的HTML

<html ng-app='CoreAngular_App'>
<script>
    var CoreAngular_AppObj = angular.module('CoreAngular_App', ['importantModule1', 'importantModule2', 'customModule1', 'customModule2', 'customModule3']);
</script>
<!-- here goes html structure with lot of ng-controller -->
<script>
    //controller example
    CoreAngular_AppObj.controller('MyControllerX', function($scope, myCustomModule2) {
    }
</script>
</html>

它有效,但由于我有很多控制器,而且我不知道它们将使用哪些,我想动态加载此列表['customModule1', 'customModule2', 'customModule3']

我很高兴得到这样的东西。

<html ng-app='CoreAngular_App'>
<script>
    var CoreAngular_AppObj = angular.module('CoreAngular_App', ['importantModule1', 'importantModule2']);
</script>
<!-- here goes html structure with lot of ng-controller -->
<script>
    // ADD myCustomModule2 TO CoreAngular_AppObj HERE
    //controller example
    CoreAngular_AppObj.controller('MyControllerX', function($scope, myCustomModule2) {
    }
</script>
</html>

有可能吗?如何?
感谢

1 个答案:

答案 0 :(得分:1)

我试着举一个可能看起来像什么的例子。

内联 - 来自后端逻辑 -

// Create list from rendered array.
var modules = ['customModule1', 'customModule3'];  // ex: print json_encode($arrayStr);
// Add a custom module
angular.module('myCustom', modules);

//Inject the custom module
angular.module('CoreApp', ['importantModule1', 'importantModule2', 'myCustom']);

angular.bootstrap docs

请务必删除ng-app

var app = angular.module('CoreApp', []);

someReadyFunction(){
    // figure out which moules here 
    angular.bootstrap(document, ['CoreApp', 'customModule1', 'customModule3']);
} 
相关问题