首页 > 解决方案 > Angularjs:显示旧值的变量范围

问题描述

我将 userId 存储在 loginUser 函数中,然后在 getData 函数中使用该 userId ,但我仍然得到未定义的值。

另外,请建议一些关于 AngularJs 中范围的参考。

myModule.controller("loginController",function($scope,$http){
$scope.userId; //define userId

$scope.token;   

$scope.loginUser = function ()
            {
                $http.post(
                    'http://localhost:3000/museum/login',
                    {"email":$scope.username , "password":$scope.password}
                )
                .then(function(response){
                    if(response.status == 200 )
                    {
                        $scope.token = response.data.token;
                        $scope.userId = response.data.userId; //change Value of userId
                        alert ("Login Successful" + $scope.userId);
                        window.open("updateinfo.html","_self");
                    }
                   else{
                           alert ("Login Fail");    
                       }

                });
            };


 $scope.getData = function()
    {
        console.log("Function Called " +$scope.userId); //using userId

        $http.get(
                  'http://localhost:3000/museum/museumDetail/'+$scope.userId,
                  {}
                 )
                 .then(function(response)
                 {
                   console.log(response.data);
                 });
       };
 });

标签: angularjsangularjs-scope

解决方案


推荐阅读