簡體   English   中英

更改后角度$ location將返回

[英]Angular $location is going back after change

我正在使用Angular,我需要在用戶提交表單后將用戶重定向到視圖頁面。

我已將以下功能附加到范圍:

$scope.create = function () {
   return $scope.customer.$save({}, function (obj) {
       Msg.show('Customer created');
       return $location.path('customer/'+obj.id);
   })
};

這一項工作正常,用戶已重定向。

我創建了一個服務來封裝邏輯。

...
this.create = function (object, params, callback) {
    return function () {
        return object.$save(params, function (obj) {
            Msg.show('Object created');
            if ( callback instanceof Function ) {
                return callback(obj);
            }
        });
    };
};
....

所以我將其附加到這樣的范圍:

$scope.create = ResourceActions.create($scope.customer, {}, function (customer) {
    return $location.path('customers/'+customer.id);
});

提交后,用戶僅“眨眼”一秒鍾就被“重定向”,然后路徑變回原位。 不知道發生了什么。

您說過, After submit the user is "redirected" for only a blink of a second, then the path changes back. ,所以我認為應該將其重定向到錯誤的路徑,然后轉到else分支:

.otherwise({
    redirectTo: '/**'
})  

我建議您重新檢查您的customer.id如果未定義)。

我會嘗試將您的$ scope更改為:

$scope.create = ResourceActions.create($scope.customer, {}, function (customer) {
    $location.path('customers/'+customer.id);
});

另外,嘗試$ state.go。 例如,假設您具有以下狀態:

.state('customers-details', {url: '/customers/:id'}) 

因此,您可以像這樣使用$ state.go:

$scope.create = ResourceActions.create($scope.customer, {}, function (customer) {
     $state.go('customers-details', {id: customer.id});
});

記住,您必須在控制器中注入$ state。

再見

暫無
暫無

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

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