Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何刷新整个范围_Angularjs - Fatal编程技术网

Angularjs 如何刷新整个范围

Angularjs 如何刷新整个范围,angularjs,Angularjs,我尝试在移动应用程序中实现身份验证。基本上,用户单击一个按钮,该按钮在InAppBrowser中打开一个URL,当身份验证完成时,该URL将关闭。然后通过调用RESTURI刷新当前用户。最后,我用新数据刷新$scope。问题是并不是整个范围都被刷新了 $scope.user被刷新,但不刷新$scope.userLoggedIn,这取决于$scope.user resolve: { user: ['User', function (User) { return User.c

我尝试在移动应用程序中实现身份验证。基本上,用户单击一个按钮,该按钮在InAppBrowser中打开一个URL,当身份验证完成时,该URL将关闭。然后通过调用RESTURI刷新当前用户。最后,我用新数据刷新
$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;
}
$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);
});

我设法做到了。我需要使用
$watch
来监视对
$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;
}
$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);
});