Angularjs $scope变量在解析更新时未更新
我正在使用ionic和parse构建一个应用程序。我正在基于单击更新解析中的布尔值。一切都在解析端工作,我看到在函数运行后控制台中更新了用户对象,但是scope变量在用户注销、返回页面之前不会更新,然后通常还要再次刷新才能看到$scope.isInstagramLinked更新为其真实值 控制器Angularjs $scope变量在解析更新时未更新,angularjs,parse-platform,ionic-framework,angularjs-scope,Angularjs,Parse Platform,Ionic Framework,Angularjs Scope,我正在使用ionic和parse构建一个应用程序。我正在基于单击更新解析中的布尔值。一切都在解析端工作,我看到在函数运行后控制台中更新了用户对象,但是scope变量在用户注销、返回页面之前不会更新,然后通常还要再次刷新才能看到$scope.isInstagramLinked更新为其真实值 控制器 var app = angular.module('myApp.controllers.account', []); app.controller('AccountCtrl', function ($
var app = angular.module('myApp.controllers.account', []);
app.controller('AccountCtrl', function ($scope, $state, $cordovaOauth, AuthService) {
$scope.isInstagramLinked = AuthService.user.attributes.is_instagram_linked;
$scope.linkInstagram = function() {
$cordovaOauth.instagram('######', [], {})
.then(function(result) {
console.log("Response Object -> " + JSON.stringify(result));
console.log(result.access_token);
// save the access token & get user info
AuthService.setInstagramAccessToken(result.access_token).then(function() {
console.log('Token saved!');
});
}, function(error) {
console.log("Error -> " + error);
});
}
$scope.unlinkInstagram = function() {
AuthService.removeInstagramInfo().then(function() {
console.log('Insta unlinked');
console.log(AuthService.user.attributes);
});
}
});
服务
var app = angular.module('myApp.services.authentication', []);
app.service('AuthService', function ($q, $http, $ionicPopup) {
var self = {
user: Parse.User.current(),
'setInstagramAccessToken': function(token) {
var d = $q.defer();
var user = self.user;
user.set("instagram_access_token", token);
user.save(null, {
success: function(user) {
self.user = Parse.User.current();
d.resolve(self.user);
},
error: function(user, error) {
$ionicPopup.alert({
title: "Save Error",
subTitle: error.message
});
d.reject(error);
}
});
self.setInstagramUserInfo(token);
return d.promise;
},
'setInstagramUserInfo': function(token) {
var d = $q.defer();
var endpoint = 'https://api.instagram.com/v1/users/self?access_token=' + token + '&callback=JSON_CALLBACK';
$http.jsonp(endpoint).then(function(response) {
console.log(response.data.data.username);
console.log(response.data.data.id);
var user = self.user;
user.set('is_instagram_linked', true);
user.set('instagram_username', response.data.data.username);
user.set('instagram_user_id', response.data.data.id);
user.save(null, {
success: function(user) {
self.user = Parse.User.current();
d.resolve(self.user);
},
error: function(user, error) {
$ionicPopup.alert({
title: "Save Error",
subTitle: error.message
});
d.reject(error);
}
});
});
},
'removeInstagramInfo': function() {
var d = $q.defer();
var user = self.user;
user.set('is_instagram_linked', false);
user.set('instagram_access_token', null);
user.set('instagram_username', null);
user.set('instagram_user_id', null);
user.save(null, {
success: function(user) {
self.user = Parse.User.current();
d.resolve(self.user);
},
error: function(user, error) {
$ionicPopup.alert({
title: "Save Error",
subTitle: error.message
});
d.reject(error);
}
});
return d.promise;
}
};
return self;
});
我在函数末尾尝试了类似的操作,但出现了一个错误,错误是:[$rootScope:inprog]$digest已经在进行中
$scope.$apply(function () {
$scope.isInstagramLinked = false;
});
我猜你是在假设下面这句话
$scope.isInstagramLinked = AuthService.user.attributes.is_instagram_linked;
将对AuthService.user.attributes.is\u instagram\u linked进行“$scope.IsInstagramLink”随时更新。但事实并非如此。因为“AuthService.user.attributes.is\u instagram\u linked”引用了一个基元(布尔)值,所以它只分配给它—它不维护对它的任何类型的引用—这只发生在对象上
您需要在$cordovoauth.instagram()success/“then”处理程序中手动设置$scope.isInstangramLinked=true
tl;博士:
$scope.isLinked = false;
someFunction().then(function(){
$scope.isLinked = true; // this is what you're missing
})
.error(function(err){...})
如果不想手动设置,还可以使用监视“AuthService.user.attributes.is\u instagram\u linked”的更改,然后在更改时更新“$scope.isInstagramLinked”