我有以下代码:titledbApp.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";
};
}]);
最佳答案
titledbApp.controller('TitleListController', ['$cookieStore', function($cookieStore, $scope, $http)
应该是
titledbApp.controller('TitleListController', ['$cookieStore', '$scope', '$http', function($cookieStore, $scope, $http)
您必须先将它们全部注入(inject),然后才能在代码中访问它们。
关于javascript - AngularJS - Controller 功能顺序 - 一些引用文献不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40134939/