search

Home  >  Q&A  >  body text

angular.js - angular.module中模块的名字一定要和ng-app属性的值一样吗

例如ng-app="app",如果angular.module("app",[]);中的名字不定义为"app"的话会报错。

PHPzPHPz2744 days ago1039

reply all(5)I'll reply

  • 怪我咯

    怪我咯2017-05-15 17:09:57

    ng-app is the entrance to the entire angular application. It will find the corresponding angular module according to the name specified by ng-app. If it is inconsistent, the corresponding module cannot be found for initialization. Therefore, the root module name of the application must be consistent with the name specified by ng-app

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-15 17:09:57

    ng-app is the entrance to the entire application, so it must be consistent with the module name of the entrance. An application can only have one and only ng-app

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-15 17:09:57

    In an angular application, there can be multiple angular.modules. There should be one and only one angular.module whose name is consistent with the value of ng-app, otherwise it will be meaningless.

    angular.module('M1',[]);
    angular.module('M2',[]);
    ......
    angular.module('Mn',[]);
    
    angular.module('app',['M1','M2',...,'Mn']);
    
    

    M1, M2,...,Mn may be different business modules, which can be used as an angular.module alone, and finally all are mounted under the app module.

    ----------------------------------Separating line---------- ---------------------------------------

    The above is automatically loaded. If you use manual loading, there is no name limit or quantity limit. app

    <!DOCTYPE html>
    <html>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <body>
    
        <p id="app1">
            <p ng-controller="myCtrl">
                {{ hello }}
            </p>
        </p>
        
        <p id="app2">
            <p ng-controller="myCtrl">
                {{ hello }}
            </p>
        </p>
    
        <script type="text/javascript">
            var app1 = angular.module("test1",[]);
            app1.controller("myCtrl",function($scope){
                $scope.hello = "a Angular app";
            });
            
    
            var app2 = angular.module("test2",[]);
            app2.controller("myCtrl",function($scope){
                $scope.hello = " another Angular app";
            });
    
            angular.bootstrap(document.getElementById("app1"),['test1']);
            angular.bootstrap(document.getElementById("app2"),['test2']);
        </script>
    </body>
    </html>
    

    Rendering


    The above example starts two angular apps without using the

    directive. ng-app

    reply
    0
  • 迷茫

    迷茫2017-05-15 17:09:57

    Thank you for your patient answers

    reply
    0
  • 为情所困

    为情所困2017-05-15 17:09:57

    It must be the same because this is the most important angular binding

    reply
    0
  • Cancelreply