Angular 范围值无法从同一控制器中的另一个函数访问
Angular scope value can't access from another function in the same controller
在我的 Angular 控制器中有两个 functions.Normally 它应该能够从其他函数访问 $scope 值,因为它在同一个 controller.But 我无法访问 it.I 在两个 function.First 函数中添加了两个 console.log() 检索范围值,因为我可以看到它在第二个函数中打印 console.But 中的值,它显示未定义的 variable.Hope 有人会帮我解决这个问题。
以下是我的代码,
第一个函数
//Get user id
$scope.getUserId = function() {
UserService.getCurrentUser().then(function(response) {
if (response.query_status == "success") {
$scope.userid = response.data.id;
console.log('User id is: '+ $scope.userid);
}
else {
$scope.userid = null;
}
}, function(error) {
$scope.userid = null;
console.log("Error : Failed to load user data...");
console.log(error);
});
}
第二个函数
//Access user id
$scope.accessUserId = function() {
console.log('User id from other function is:'+ $scope.userid);
}
我可以解决 this.I 需要使用 Angular $q.defer() method.There 处理其他函数的响应有些延迟。
在我的 Angular 控制器中有两个 functions.Normally 它应该能够从其他函数访问 $scope 值,因为它在同一个 controller.But 我无法访问 it.I 在两个 function.First 函数中添加了两个 console.log() 检索范围值,因为我可以看到它在第二个函数中打印 console.But 中的值,它显示未定义的 variable.Hope 有人会帮我解决这个问题。
以下是我的代码,
第一个函数
//Get user id
$scope.getUserId = function() {
UserService.getCurrentUser().then(function(response) {
if (response.query_status == "success") {
$scope.userid = response.data.id;
console.log('User id is: '+ $scope.userid);
}
else {
$scope.userid = null;
}
}, function(error) {
$scope.userid = null;
console.log("Error : Failed to load user data...");
console.log(error);
});
}
第二个函数
//Access user id
$scope.accessUserId = function() {
console.log('User id from other function is:'+ $scope.userid);
}
我可以解决 this.I 需要使用 Angular $q.defer() method.There 处理其他函数的响应有些延迟。