Javascript AngularJS-控制器函数顺序-某些参考不起作用
我有以下代码:Javascript AngularJS-控制器函数顺序-某些参考不起作用,javascript,angularjs,Javascript,Angularjs,我有以下代码:titledbap.controller('TitleListController',['$cookieStore',function($scope,$http,$cookieStore){ 它不会工作-它基本上可以检测$CookieStore并说它是未定义的,所以所有.get和.put请求都会失败 当我将function()中的$cookieStore移动到开始位置(在$scope之前)时,它工作正常,但是$scope和$http无法工作 完整代码: titledbApp.con
titledbap.controller('TitleListController',['$cookieStore',function($scope,$http,$cookieStore){
它不会工作-它基本上可以检测$CookieStore并说它是未定义的
,所以所有.get和.put请求都会失败
当我将function()
中的$cookieStore
移动到开始位置(在$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";
};
}]);
titledbap.controller('TitleListController',['$cookieStore',函数($cookieStore,$scope,$http){
$cookieStore.put('ETag','test');
var-etag='t';
$http.get('https://api.github.com/users/ImReallyShiny/repos“,{headers:{'If-None-Match':'t'}})。然后(函数successCallback(响应,headers){
$cookieStore.put('ETag',headers('ETag');
$scope.titles=response.data;
$scope.titles.拼接(1,1);
$scope.titles.sort(函数a、b){
if(a.name.toUpperCase()b.name.toUpperCase())返回1;
返回0;
});
}),函数errorCallback(){
返回“错误”;
};
}]);
应该是
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";
};
}]);
titledbap.controller('TitleListController',['$cookieStore','$scope','$http',函数($cookieStore,$scope,$http){
$cookieStore.put('ETag','test');
var-etag='t';
$http.get('https://api.github.com/users/ImReallyShiny/repos“,{headers:{'If-None-Match':'t'}})。然后(函数successCallback(响应,headers){
$cookieStore.put('ETag',headers('ETag');
$scope.titles=response.data;
$scope.titles.拼接(1,1);
$scope.titles.sort(函数a、b){
if(a.name.toUpperCase()b.name.toUpperCase())返回1;
返回0;
});
}),函数errorCallback(){
返回“错误”;
};
}]);
您还可以使用函数语法,
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";
};
}
titledbap.controller('TitleListController',TitleListController);
函数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'}})。然后(函数successCallback(响应,headers){
$cookieStore.put('ETag',headers('ETag');
$scope.titles=response.data;
$scope.titles.拼接(1,1);
$scope.titles.sort(函数a、b){
if(a.name.toUpperCase()b.name.toUpperCase())返回1;
返回0;
});
}),函数errorCallback(){
返回“错误”;
};
}
应该是['$cookieStore'、'$scope'、'$http',函数($cookieStore,$scope,$http),顺序应该一点也不重要,只要你保持一对一。我不是100%,但在函数之前,你有'$cookieStore',你不需要以同样的方式注入$scope和$http吗?谢谢大家,它已经完美地解决了:)文档是。查找内联数组注释
我的错!对不起,我对AngularJS很陌生-我感谢你的帮助,而不是仅仅在6分钟内否决和忽略:)接受。感谢你真的把时间和想法投入到一个格式良好的问题中。这里的大多数人都非常乐于助人,假设你把不管这个问题有多“愚蠢”,你都要为此付出一些努力。
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";
};
}