如何创建一个简单的模块来注入应用模块

how to create a simple module to inject application module?

本文关键字:模块 注入 应用 简单 何创建 创建 一个      更新时间:2023-09-26

如何创建一个带有模板"Hello world"指令的模块,并在初始化网页时将该模块注入基模块(应用程序),该指令应该被编译并附加到<body> ?

angular.module('helloWorld',[]).provider(['helloWorld',function(ngModalView){
    return{
        $get : [function($compile, $scope){
             var tem = $compile('<hello-world></hello-world>')($scope);
             //append this template to body
        }]
    };
}]);
angular.module('helloWorld',[]).directive('helloWorld',[function(){
        return {
            restrict: 'E',      
            replace: true,
            template: '<div style="width=150px;height="150px">Hello world</div>'  
        };
}]);

我需要在应用程序启动时运行上面的模块。

angular.module('app',['helloWorld']);

每当angular应用启动时,它首先会加载依赖模块(在你的例子中是"helloWorld"),然后加载主模块"app"。所以你不用担心加载/运行依赖的模块。

如果你正在考虑延迟加载模块,那么看看这个urls

https://oclazyload.readme.io/