AngularJS -控制器函数顺序-一些引用不起作用
AngularJS - Controller Function Order - Some References wont work
我有以下代码:titledbApp.controller('TitleListController', ['$cookieStore', function($scope, $http, $cookieStore) {
它不会工作-它基本上不能检测$CookieStore,并说它是undefined
,所以所有的。get和。put请求失败。
当我移动$cookieStore
在function()
开始($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";
};
}
相关文章:
- 如果视图中没有Jquery脚本引用,Unobtrusive Javascript Validation是不起作用的
- ng-view 中的脚本在从文件引用时不起作用(JQuery 在角度之前加载)
- 对jquery函数的引用不起作用
- Twitter引导程序弹出在网站上不起作用:未获取引用错误
- EJS文件中的Javascript引用在Webstorm上不起作用
- 通过引用进行的Javascript调用不起作用
- 引用 jQuery UI 不起作用
- Jquery 选项卡不起作用或引用错误
- 循环引用似乎在事件处理程序函数中不起作用
- Jquery 和闭包或函数引用不起作用
- 为什么我的deffered()函数不起作用,除非我引用它的另一个变量
- 跟踪像素应该通过引用页面加载外部网站 - 但它不起作用
- XHR 上传功能的单例引用不起作用
- 选择器 $(body) 有效但破坏了代码(引用错误),而 $(“body”) 没有显示错误但不起作用
- Angular:脚本引用不起作用
- AngularJS -控制器函数顺序-一些引用不起作用
- 为什么我的jQuery引用不起作用?我引用的脚本不正确
- Javascript:没有子级的脚本引用不起作用
- RegExp替换函数中的反向引用不起作用
- 对此的引用不起作用