AngularJS -控制器函数顺序-一些引用不起作用

AngularJS - Controller Function Order - Some References wont work

本文关键字:引用 不起作用 顺序 控制器 函数 AngularJS      更新时间:2023-09-26

我有以下代码:titledbApp.controller('TitleListController', ['$cookieStore', function($scope, $http, $cookieStore) {

不会工作-它基本上不能检测$CookieStore,并说它是undefined,所以所有的。get和。put请求失败。

当我移动$cookieStorefunction()开始($scope之前)它工作正常,但$scope$http无法工作。

完整代码:

titledbApp.controller('TitleListController', ['$cookieStore', function($cookieStore, $scope, $http) {
    $cookieStore.put('ETag', 'test');
    var etag = 't';
    $http.get('https://api.github.com/users/ImReallyShiny/repos', {headers: {'If-None-Match': 't'}}).then(function successCallback(response, headers) {
        $cookieStore.put('ETag', headers('ETag'));
        $scope.titles = response.data;
        $scope.titles.splice(1, 1);
        $scope.titles.sort(function(a, b){
            if(a.name.toUpperCase() < b.name.toUpperCase()) return -1;
            if(a.name.toUpperCase() > b.name.toUpperCase()) return 1;
            return 0;
        });
        }), function errorCallback() {
        return "Error";
        };
}]);
titledbApp.controller('TitleListController', ['$cookieStore', function($cookieStore, $scope, $http)
应该

titledbApp.controller('TitleListController', ['$cookieStore', '$scope', '$http', function($cookieStore, $scope, $http)

你必须把它们全部注入,然后才能在你的代码中访问它们。

根据angular js官方文档,依赖项的参数顺序和编号应该是相同的。

titledbApp.controller('TitleListController', ['$cookieStore','$scope', '$http', function($cookieStore, $scope, $http) {
    $cookieStore.put('ETag', 'test');
    var etag = 't';
    $http.get('https://api.github.com/users/ImReallyShiny/repos', {headers: {'If-None-Match': 't'}}).then(function successCallback(response, headers) {
            $cookieStore.put('ETag', headers('ETag'));
        $scope.titles = response.data;
        $scope.titles.splice(1, 1);
        $scope.titles.sort(function(a, b){
            if(a.name.toUpperCase() < b.name.toUpperCase()) return -1;
            if(a.name.toUpperCase() > b.name.toUpperCase()) return 1;
            return 0;
        });
        }), function errorCallback() {
        return "Error";
        };
}]);

也可以使用函数语法

titledbApp.controller('TitleListController', TitleListController);
function TitleListController($cookieStore,$scope, $http){
    $cookieStore.put('ETag', 'test');
    var etag = 't';
    $http.get('https://api.github.com/users/ImReallyShiny/repos', {headers: {'If-None-Match': 't'}}).then(function successCallback(response, headers) {
            $cookieStore.put('ETag', headers('ETag'));
        $scope.titles = response.data;
        $scope.titles.splice(1, 1);
        $scope.titles.sort(function(a, b){
            if(a.name.toUpperCase() < b.name.toUpperCase()) return -1;
            if(a.name.toUpperCase() > b.name.toUpperCase()) return 1;
            return 0;
        });
        }), function errorCallback() {
        return "Error";
        };
}