angular.js - angular.module中模块的名字一定要和ng-app属性的值一样吗
PHPz
PHPz 2017-05-15 17:07:57
0
5
1034

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

PHPz
PHPz

学习是最好的投资!

reply all(5)
刘奇

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

大家讲道理

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

習慣沉默

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

迷茫

Thank you for your patient answers

为情所困

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template