繁体   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