Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何启动控制器?_Angularjs - Fatal编程技术网

Angularjs 如何启动控制器?

Angularjs 如何启动控制器?,angularjs,Angularjs,在脚本标记中尝试使用不同的语法启动控制器时,我遇到了一个问题 为什么这样做有效: function ListCtrl($scope, Projects) { $scope.projects = Projects; } 这不是: myProject.controller('ListCtrl', ['$scope', 'Projects', function ($scope, Projects) { $scope.projects = Projects; }]); 这是一个完整的

在脚本标记中尝试使用不同的语法启动控制器时,我遇到了一个问题

为什么这样做有效:

function ListCtrl($scope, Projects) {
    $scope.projects = Projects;
}
这不是:

myProject.controller('ListCtrl', ['$scope', 'Projects', function ($scope, Projects) {
    $scope.projects = Projects;
}]);
这是一个完整的plunker

提前感谢,,
-Jan

使用.strong>控制器语法时,还需要将使用函数引用的所有路由更改为使用字符串引用

有趣的是:在定义全局控制器函数时,使用字符串引用也会起作用,但当前的最佳实践是使用.controller语法并避免使用全局函数

var myProject = angular.module('project', ['firebase']).
        value('fbURL', 'https://angularjs-projects.firebaseio.com/').
        factory('Projects', function(angularFireCollection, fbURL) {
            return angularFireCollection(fbURL);
        }).
        config(function($routeProvider) {
            $routeProvider.
                    when('/', {controller:'ListCtrl', templateUrl:'list.html'}).
                    otherwise({redirectTo:'/'});
        });

// function ListCtrl($scope, Projects) {
//     $scope.projects = Projects;
// }

// next 3 lines will work
myProject.controller('ListCtrl', ['$scope', 'Projects', function ($scope, Projects) {
   $scope.projects = Projects;
}]);

你们现在也可以投票了,祝贺你们,欢迎来到SO