簡體   English   中英

$ scope var在Parse更新時未更新

[英]$scope var not updating on Parse update

我正在使用ionic和parse構建應用程序。 我正在基於單擊更新解析中的布爾值。 一切都在解析端工作,在函數運行后,我看到控制台中的用戶對象已更新,但是scope變量直到用戶注銷並返回頁面才更新,然后通常甚至必須再次刷新才能看到$ scope.isInstagramLinked已更新為真實值。

控制者

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_instagram_linked”更新時,都會進行“ $ scope.isInstagramLinked”更新。 事實並非如此。 因為“ AuthService.user.attributes.is_instagram_linked”引用的是原始值(布爾值),所以它只是為其分配值(它不維護對它的任何形式的引用),僅在對象中發生。

您需要在$ cordovaOauth.instagram()success /“ then”處理程序中手動設置$ scope.isInstangramLinked = true。

tl; dr:

$scope.isLinked = false;

someFunction().then(function(){
   $scope.isLinked = true; // this is what you're missing
})
.error(function(err){...})

如果您不想手動設置它,則還可以使用$ scope。$ watch監視'AuthService.user.attributes.is_instagram_linked'的更改,然后在執行時更新'$ scope.isInstagramLinked'。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM