18

ユーザーが正しいユーザー名とパスワードを入力したら、別の場所にリダイレクトします。しかし、$location.path('dashboard')ここで使用すると、ブラウザーの URL が変更されましたが、そのページは読み込まれません。ctrl+R を使用してページを更新するか、ブラウザーの更新アイコンをクリックすると、適切なページが読み込まれます。

$http.post('/login', $scope.userInfo)
        .success(function (data) {
            //Check if the authentication was a success or a failure
            //Successful POST here does not indicate success of authentication
            if (data.status === "authentication success") {

                //Proceed to load the dashboard for the user                    
                $location.path('/dashboard');

            } else if (data.status === "authentication error") {
                //Inform user of the error
                $scope.errorMessage = data.error;
                $scope.showErrorMessage = true;
            }

        })
        .error(function (error) {
            $scope.errorMessage = "Error while attempting to authenticate. Check  log.";
            $scope.showErrorMessage = true;

        });
    };

}]);
4

7 に答える 7

24

その後 $scope.$apply を使ってみましたか?例えば。:

if(!$scope.$$phase) $scope.$apply()

この短い方法は、非常に頻繁に役立ちます。

https://github.com/yearofmoo/AngularJS-Scope.SafeApply

于 2013-11-28T13:21:31.787 に答える
14

https://docs.angularjs.org/guide/$locationを引用するには

「$location サービスでは、URL のみを変更できます。ページをリロードすることはできません。URL を変更してページをリロードするか、別のページに移動する必要がある場合は、下位レベルの API である $window を使用してください。 .location.href."

したがって、たとえば、次を使用する代わりに: $location.path("/path/to/something");

これを使って: $window.location.href = "/path/to/something";

$window.location.hrefURL を変更し、新しいページを読み込みます

それが役立つことを願っています。

于 2015-10-31T07:30:18.450 に答える
0

$timeout(function){} を使用するとうまくいきます

于 2017-11-06T22:41:11.947 に答える
-6

試す:

 $location.path('/dashboard');
 $location.reload();
于 2014-04-23T13:35:28.900 に答える