gpt4 book ai didi

javascript - AngularJS 'Cannot read property ' 然后'未定义'

转载 作者:行者123 更新时间:2023-12-05 00:54:10 25 4
gpt4 key购买 nike

我有这个问题,当我点击登录按钮时,chrome 控制台会记录这个:

angular.min.js:117 TypeError: Cannot read property 'then' of undefined at m.$scope.logIn (loginModuleController.js:11)



服务:
angular.module('loginModule')
.factory('loginService', function($http){
return{
login: function(username, password){
var session;
$http.post('../server/php/auth/auth.php', {
username: username,
password: password
})
.then(function(res){
session = res;
});
return session;
},
isLogged: function(){
return $http.get('../angCMS/server/php/auth.php?is_logged=true');
},
logOut: function(){
return $http.get('../angCMS/server/php/auth.php?logout=true');
}
};
});

Controller :
angular.module('loginModule')
.controller('LoginCtrl', ['$scope', 'loginService', function($scope, loginService){

$scope.auth = false;

$scope.username;
$scope.password;
$scope.logIn = function(){
loginService.login($scope.username, $scope.password).then(function(response){

}, function(res){

});
};

$scope.isLogged = function(){
loginService.isLogged()
.then(function(response){
if(response){
$scope.auth = true;
}
});
};

$scope.logOut = function(){
loginService.logOut()
.then(function(response){
if(response){
$scope.auth = false;
}
});
};

}]);

这是 html 模板:
<div class="container" ng-if="auth==false">
<div class="col-md-4 col-md-offset-4">
<div class="row">
<br/><h2 align="center">Login</h2>
</div>
<div class="well">
<form class="form-horizontal">
<fieldset>
<div class="form-group">
<input type="text" class="form-control" placeholder="Username" ng-model="username" required>
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Password" ng-model="password" required>
</div>
<div class="form-group">
<button class="btn btn-md btn-primary btn-block" type="submit" ng-click="logIn()">Sign in</button>
</div>
</fieldset>
</div>
</form>
</div>
</div>

PHP登录方法:
public function login($user, $pass){

$user = htmlspecialchars(trim($user));
$pass = md5(htmlspecialchars(trim($pass)));

$res = $this->DB->prepare("SELECT * FROM `admin` WHERE username = :user");
if(!$res->execute(Array(":user"=>$user)))
die(mysql_error());

$row = $res->fetch(PDO::FETCH_ASSOC);

if(!$row['password'] == $pass)
die("Errore: password errata!");

$_SESSION['logged'] = $row;
array_push($session, $_SESSION['logged'], true);

return $session;
}

最佳答案

这更像是一个滥用 promise 的问题。

你可能想先看看 Promise 是如何工作的:

  • https://docs.angularjs.org/api/ng/service/ $q
  • https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

  • 从您的服务代码:
    login: function(username, password){
    var session;
    $http.post('../server/php/auth/auth.php', {
    username: username,
    password: password
    })
    .then(function(res){
    session = res;
    });
    return session;
    }

    login(username, password)被调用, session 仍然是 undefined回来后。

    这是因为 $http.post()是一个异步函数, .then()子句不会立即执行。

    正如 Snixtor's answer 所指出的,
    你应该“返回 $http.post() 的返回值”:
    login: function(username, password){
    return $http.post('../server/php/auth/auth.php', {
    username: username,
    password: password
    });
    }

    然后引用您的 Controller 的登录方法:
    $scope.logIn = function(){
    loginService.login($scope.username, $scope.password).then(function(response){
    // response === 'session'
    }, function(res){

    });
    };
    response来自 loginService.login().then() 的参数正是您预期的值(value) session来自您之前实现的变量。

    关于javascript - AngularJS 'Cannot read property ' 然后'未定义',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37955434/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com