如何刷新整个范围

时间:2014-05-18 09:17:13

标签: angularjs

我尝试在移动应用中实施身份验证。基本上,用户单击一个按钮,该按钮将打开InAppBrowser中的URL,该URL在身份验证完成时关闭。然后我通过调用REST URI刷新当前用户。最后,我用新数据刷新$scope。问题是不是整个范围都要刷新。

$scope.user已刷新,但$scope.userLoggedIn取决于$scope.user

resolve: {
    user: ['User', function (User) {
        return User.current(function (user) {
            return user;
        }, function (error) {
            return {};
        });
    }
    ]
},
controller: function ($scope, $window, user, $translate, $parse) {
    $scope.user = user;

    $scope.redirectTo = function (path) {
        var x = $window.open(path, '_blank')
        x.addEventListener('loadstop', function(event) { 
            // $scope.$apply(function(){ //let angular know the changes
                alert('loadStop: ' + event.url); 
                var url = event.url;
                var filename = url.substring(url.lastIndexOf('/')+1);
                if(filename == "mobile.login.html"){
                    x.close();
                    alert("refresh current user");
                    user.$current(function (data) {
                            alert(data);
                            $scope.user = data;
                            alert(data);
                            alert($scope.userLoggedIn);
                        });
                }
        //  });
        });
        //$window.location.href = path;
    };

    $scope.userLoggedIn = $scope.user.profile !== undefined;
}

1 个答案:

答案 0 :(得分:1)

我设法让它发挥作用。我需要使用$watch来监控对$scope.user的更改。

$scope.$watch('user', function(newValue, oldValue) {
    alert("New Value" + $filter('json')(newValue));
    alert("Old Value" + $filter('json')(oldValue));
    $scope.userLoggedIn = newValue.profile !== undefined;
    alert("user changed " + newValue.profile + " " + $scope.userLoggedIn);
});