无法使用 Angular 读取未定义的 属性 'get'
Cannot read property 'get' of undefined with Angular
我正在尝试使用 angular 构建一个简单的身份验证,它在进行 ajax 服务调用时出现此错误。这是代码。
代码
mainModule.controller("loginController", ['$scope', '$http', '$rootScope', '$presence', '$apps', '$helpers',
function($scope, $http, $rootScope, $presence, $apps, $helpers) {
$scope.currentUser;
$scope.password;
$rootScope.url = "http://localhost:53455/eSuperVision.svc";
$scope.login = function($scope, $http) {
$http.get($rootScope.url + "/login/" + $scope.currentUser + "/" + $scope.password).success(function(response) {
if (response == true) {
location.href = "home.html";
} else {
$("#loginDialog").effect("shake");
$scope.showError = true;
}
});
}
}
]);
因为您在登录函数中使用的 $http 不是注入控制器中的 $http 服务,而是传递给该函数的任意参数。
只需删除它:$scope.login = function() {...}
你的函数不应该传递 $http
和 $scope
的参数,它们被函数覆盖并且它们都未定义。您应该使用从控制器注入的依赖项。
代码
$scope.login = function() {
$http.get($rootScope.url + "/login/" + $scope.currentUser + "/" + $scope.password).success(function(response) {
if (response == true) {
location.href = "home.html";
} else {
$("#loginDialog").effect("shake");
$scope.showError = true;
}
});
}
我正在尝试使用 angular 构建一个简单的身份验证,它在进行 ajax 服务调用时出现此错误。这是代码。
代码
mainModule.controller("loginController", ['$scope', '$http', '$rootScope', '$presence', '$apps', '$helpers',
function($scope, $http, $rootScope, $presence, $apps, $helpers) {
$scope.currentUser;
$scope.password;
$rootScope.url = "http://localhost:53455/eSuperVision.svc";
$scope.login = function($scope, $http) {
$http.get($rootScope.url + "/login/" + $scope.currentUser + "/" + $scope.password).success(function(response) {
if (response == true) {
location.href = "home.html";
} else {
$("#loginDialog").effect("shake");
$scope.showError = true;
}
});
}
}
]);
因为您在登录函数中使用的 $http 不是注入控制器中的 $http 服务,而是传递给该函数的任意参数。
只需删除它:$scope.login = function() {...}
你的函数不应该传递 $http
和 $scope
的参数,它们被函数覆盖并且它们都未定义。您应该使用从控制器注入的依赖项。
代码
$scope.login = function() {
$http.get($rootScope.url + "/login/" + $scope.currentUser + "/" + $scope.password).success(function(response) {
if (response == true) {
location.href = "home.html";
} else {
$("#loginDialog").effect("shake");
$scope.showError = true;
}
});
}