Javascript 未定义的AngularJS常量不是对象
Angular是个新手,我正在尝试从一个单独的文件中注入常量。它似乎适用于DI,但当我尝试使用它时,我得到一个错误:Javascript 未定义的AngularJS常量不是对象,javascript,angularjs,dependency-injection,angularjs-controller,Javascript,Angularjs,Dependency Injection,Angularjs Controller,Angular是个新手,我正在尝试从一个单独的文件中注入常量。它似乎适用于DI,但当我尝试使用它时,我得到一个错误:错误:undefined不是一个对象(评估'CApiEndpoints.authUrl') 我尝试了中建议的点符号和括号,但仍然出现错误 这些文件包含在index.html中,DI没有抱怨 index.html <script type="text/javascript" src="js/angular/constants.js"></script> <
错误:undefined不是一个对象(评估'CApiEndpoints.authUrl')
我尝试了中建议的点符号和括号,但仍然出现错误
这些文件包含在index.html中,DI没有抱怨
index.html
<script type="text/javascript" src="js/angular/constants.js"></script>
<script type="text/javascript" src="js/angular/app.js"></script>
还有我的js/angular/app.js
var app = angular.module('app', ['ngRoute', 'ngCookies', 'appConst', 'appServices']);
app.controller('pageController', ['$scope', '$route', '$http', '$cookies', 'CApiEndpoints', function($scope, $route, $http, $cookies, $routeParams, $location, CApiEndpoints){
console.log(CApiEndpoints); // shows 'undefined'
$http({
method : 'GET',
url : CApiEndpoints.authUrl + 'user_info'
})
.then(
function successCallback(response) {
console.log(response);
},
function errorCallback(response) {
console.log(response);
});
}]);
任何帮助都将不胜感激。在过去的两个小时里,我一直在寻找答案。当使用在控制器函数中注入依赖项时,它们必须遵循在数组中注入依赖项的顺序 如果您遵循上述规则,您将知道函数中有两个额外的参数,所以您应该删除这两个不需要的(
$routeParams,$location
)依赖项
app.controller('pageController', ['$scope', '$route', '$http', '$cookies', 'CApiEndpoints',
function($scope, $route, $http, $cookies, CApiEndpoints){
//controller code
}
]);
若并没有错误地添加这些参数,那个么应该在函数和数组的两侧添加这些参数
app.controller('pageController', ['$scope', '$route', '$http', '$cookies', '$routeParams', '$location', 'CApiEndpoints',
function($scope, $route, $http, $cookies, $routeParams, $location CApiEndpoints){
//controller code
}
]);
啊!之前我把
$scope
和$http
的顺序搞乱了,我花了一段时间才弄明白。我正在使用$routeParams
和$location
,但这让我找到了解决问题的正确途径。对我来说:app.controller('pageController',['$scope','$route','$http','$cookies','CApiEndpoints',函数($scope,$route,$http,$cookies,$CApiEndpoints,$routeParams,$location){…
现在可以正常工作了。谢谢Pankaj!@FriderichWeber它不应该是..虽然你上面的代码行不应该有,$routeParams,$location
基本上它们是未定义的。不需要添加它们,我现在已经将它们全部删除了,它又开始工作了。再次感谢。@FriderichWeber很高兴帮助你..谢谢:)
app.controller('pageController', ['$scope', '$route', '$http', '$cookies', '$routeParams', '$location', 'CApiEndpoints',
function($scope, $route, $http, $cookies, $routeParams, $location CApiEndpoints){
//controller code
}
]);