【问题标题】:$scope var not updating on Parse update$scope var 未在 Parse 更新时更新
【发布时间】:2016-01-28 07:54:32
【问题描述】:

我正在使用 ionic 和 parse 构建应用程序。我正在根据点击更新解析中的布尔值。一切都在解析结束,我看到函数运行后在控制台中更新了用户对象,但是范围变量在用户注销之前不会更新,回到页面,然后通常甚至必须再次刷新才能看到$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;
    });

我在函数末尾尝试了类似的操作,但收到错误消息 Error: [$rootScope:inprog] $digest already in progress

$scope.$apply(function () {
     $scope.isInstagramLinked = false;
});

【问题讨论】:

    标签: angularjs parse-platform ionic-framework angularjs-scope


    【解决方案1】:

    我猜你假设以下行

        $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;博士:

    $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'。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多