我试图使用角种子模板与默认设置。在我使用的controllers.js
angular.module('myApp.controllers',[]). controller('MyCtrl1',[function($scope) { $scope.test = 'scope found!'; }]) .controller('MyCtrl2',[function() { }]);
$ scope总是未定义。
当我把控制器从模块中取出并在全局注册它工作正常。如下:
function MyCtrl1($scope) { $scope.test = "scope found!"; } MyCtrl1.$inject = ['$scope'];
有人可以向我解释为什么会这样吗?
你不能混合这样的东西。你需要决定两种可能性之一:
原文链接:https://www.f2er.com/angularjs/146344.htmlapp = angular.module('test',[]); // possibility 1 - this is not safe for minification because changing the name // of $scope will break Angular's dependency injection app.controller('MyController1',function($scope) { // ... }); // possibility 2 - safe for minification,uses 'sc' as an alias for $scope app.controller('MyController1',['$scope',function(sc) { // ... }]);
我不建议使用其他语法直接声明Controller。迟早或随着你的应用程序的增长将变得难以维护和跟踪。但如果你必须,有3种可能性:
function myController1 = function($scope) { // not safe for minification } function myController2 = ['$scope',function(sc) { // safe for minification,you could even rename scope }] var myController3 = function(sc) { // safe for minification,but might be hard // to read if controller code gets longer } myController3.$inject = ['$scope'];