user6846750 user6846750 - 1 month ago 19
AngularJS Question

AngularJS - Controller Function Order - Some References wont work

I have the following code:

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


It WONT work - It basically cant detect $CookieStore and says its undefined so all .get and .put requests fail.

When I move
$cookieStore
in
function()
to the start (Before
$scope
) it works fine but then
$scope
and
$http
fail to work.

Full code:

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";
};
}]);

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

Should be

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

You have to inject them all before you can access them in your code.