【发布时间】:2018-03-23 12:24:07
【问题描述】:
在用户输入正确的用户名和密码后,尽管应用了 $scope.$apply(),isV(boolean) 的值并没有改变。 $scope.$digest() 也不起作用
app.controller('validateCtrl', function($scope,$location) {
Parse.initialize("PARSE APP ID");
Parse.serverURL = 'PARSE SERVER URL'
$scope.isV="false";
$scope.submit = function(){
var username =$scope.username;
var password = $scope.password;
Parse.User.logIn(username, password, {
// If the username and password matches
success: function(user) {
alert('Welcome!');
console.log("welcome");
$scope.$apply(function(){
$scope.isV="true";
});
},
// If there is an error
error: function(user, error) {
alert(error);
}
});
console.log($scope.isV);
};
});
【问题讨论】:
-
嗨,Ashlin,感谢您的提问。当您说
$scope.$apply()不工作时,我确定您的意思是“$scope.$apply()不工作如我所料。”这是完全正常的,也是这个网站存在的原因。我每天至少会遇到几次无法按预期工作的代码。为了回答您的问题,我们需要更多信息。您目前对$scope.$apply()工作原理的理解是什么?你期待在这里发生什么?究竟发生了什么?希望这会有所帮助。 -
(^what Patrick said) 另外,您不必在 apply 函数中设置 isV 值,而是在彼此之后设置“ $scope.isV=true;$scope.$apply(); ” .此外,鉴于其异步性质,在 apply() 函数之后,您的 console.log 也应该是成功的
-
@PatrickMcElhaney 根据我的说法,如果我在 Angular 中使用第三方库,它不会起作用,我找到了一些与此相关的答案。 PS:这是我的第一篇文章,我下次会更准确。感谢您的回复。
-
@AndrewAdam 我想这就是它现在它工作正常。非常感谢。
-
@AshlinKSiby 我已经添加了我的答案作为进一步寻求建议的流浪者的答案。乐于助人:)
标签: angularjs angularjs-scope parse-javascript-sdk angularjs-apply