您的位置:首页技术文章
文章详情页

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

【字号: 日期:2024-09-24 18:19:43浏览:34作者:猪猪

问题描述

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

问题解答

回答1:

ng-app 是整个angular应用的入口,他会根据ng-app指定的名称去寻找对应的angular模块,如果不一致就无法找到对应的模块进行初始化。所以应用的根模块名称必须和ng-app指定的名称一致

回答2:

ng-app 是整个应用程序的入口,所以必须和入口的module名一致,一个应用程序只能有唯一一个ng-app

回答3:

一个angular应用中,可以有多个angular.module。应该有且只有一个angular.module的名称与ng-app的值一致,否则就没有意义了。

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

M1,M2,...,Mn可能是不同的业务模块,可以单独作为一个angular.module,最后全部挂载在app模块下。

-----------------------------------分割线---------------------------------------------------

以上是自动加载。如果采用手动加载,则不受名称限制,不受app数量限制。

<!DOCTYPE html><html> <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> <script src='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>

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

以上例子,启动了两个angular app,且没有使用ng-app指令。

回答4:

谢谢各位的耐心解答

回答5:

一定要一样的 因为这是最首要的angular绑定

相关文章: