2017-10-09 70 views
1

尝试通过电子邮件重置密码。单击电子邮件中的重置链接并指向重置页面后,控制台可以输出“$ scope.username”值,但是下一行会将错误显示为未定义的“用户名”。 Error ScreenshotAngularjs属性在控制器中未定义

不知道哪里出错了。谢谢。

.controller('resetCtrl', function(User, $routeParams, $scope, $timeout, $location) { 

    app = this; 
    app.hide = true; 

    User.resetUser($routeParams.token).then(function(data) { 
     if (data.data.success) { 
      app.hide = false; 
      app.successMsg = 'Please enter a new password'; 
      $scope.username = data.data.user.username; 
     } else { 
      app.errorMsg = data.data.message; 
     } 
    }); 

    app.savePassword = function(regData, valid, confirmed) { 
     app.errorMsg = false; 
     app.disabled = true; 
     app.loading = true; 
     console.log($scope.username); // test output 
     app.regData.username = $scope.username; 
      User.savePassword(app.regData).then(function(data) { 
       app.loading = false; 
       if (data.data.success) { 
        app.successMsg = data.data.message + '...Redirecting'; 
        $timeout(function() { 
         $location.path('/login'); 
        }, 2000); 
       } else { 
        app.disabled = false; 
        app.errorMsg = data.data.message; 
       } 
      }); 
     } 
}); 

回答

1

声明$scope.username外,将解决你的问题,因为你使用的控制器语法,最好是把它作为app.username。

.controller('resetCtrl', function(User, $routeParams, $scope, $timeout, $location) { 

    app = this; 
    app.hide = true; 
    app.username = ''; 
    User.resetUser($routeParams.token).then(function(data) { 
     if (data.data.success) { 
      app.hide = false; 
      app.successMsg = 'Please enter a new password'; 
      app.username = data.data.user.username; 
     } else { 
      app.errorMsg = data.data.message; 
     } 
    }); 

    app.savePassword = function(regData, valid, confirmed) { 
     app.errorMsg = false; 
     app.disabled = true; 
     app.loading = true; 

     app.regData.username = app.username; 
      User.savePassword(app.regData).then(function(data) { 
       app.loading = false; 
       if (data.data.success) { 
        app.successMsg = data.data.message + '...Redirecting'; 
        $timeout(function() { 
         $location.path('/login'); 
        }, 2000); 
       } else { 
        app.disabled = false; 
        app.errorMsg = data.data.message; 
       } 
      }); 
     } 
}); 
+0

谢谢Sajeetharan。但是,尽管控制台能够在错误前一行输出“app.username”的值,但问题仍然存在。 – neo

+0

你是什么意思 – Sajeetharan

+0

对不起,我没有意识到我可以通过编辑问题来插入屏幕截图。所以我使用了console.log()函数来查看该值是否已经传递给变量,实际上它的确如屏幕截图一样代码和我的代码。但是当我想使用它时,下一行显示未定义。 – neo

相关问题