Angular $位置在更改后返回

时间:2014-11-19 09:41:43

标签: javascript angularjs rest

我正在使用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);
});

提交后,用户被重定向"只有眨眼一秒钟,然后路径会变回来。不知道发生了什么。

2 个答案:

答案 0 :(得分:1)

您说After submit the user is "redirected" for only a blink of a second, then the path changes back.,所以我认为它应该重定向到错误的路径,然后转到否则分支:

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

我建议您重新检查customer.id是否可能是未定义的。

答案 1 :(得分:0)

我会尝试将您的$ 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。

再见

相关问题