看到这种写法
//app.js
angular.module('app',['app.controllers']);
//FirstController.js
angular.module('app.controllers').controller('firstCtrl',function($scope){...})
//SecondController.js
angular.module('app.controllers').controller('SecondCtrl',function($scope){...})
但是我这么写缺报 firstCtrl 和 SecondCtrl 不是一个方法,刚开始学习angular,求帮助
angular.module('app.controllers').controller('firstCtrl',function($scope){...})
这条语句的前提是你有一个view的ng-app是app.controllers。
module函数的第一个参数就是ng-app的值。
controller函数就是定义该module下的一个控制器。
angular.module('app.controllers',[])。
注意此处,一个模块首次定义的时候需要填写依赖,若没有需要填空数组。后面其他地方如果引用同一个模块,不用填写依赖。
你这里两个controller定义时,依赖都没填写,需改为如下。
angular.module('app.controllers',[]).controller('firstCtrl',function($scope){...})
angular.module('app.controllers').controller('SecondCtrl',function($scope){...})
注意:不同的依赖会生成不同的实例,因此要注意,定义完后,下次引用时一定不能再次填写依赖了,否则前一个的相关定义都会失效,这是个很难排查的bug。
关于angular风格的问题,建议参考大神https://github.com/johnpapa/angular-styleguide
建议这么写
angular
//app.js
.module('app',[])//这个'[]'用来创建依赖
//FirstController.js
angular
.module('app')//这个后面没有'[]',表面继续用之前创建的[]依赖
.controller('firstCtrl',['$scope',function($scope){//这里用[]来规范写法,防止压缩文件后看不懂形参
...
}])
//SecondController.js
angular
.module('app')//同上
.controller('SecondCtrl',['$scope',function($scope){
...
}])