简体   繁体   English

更改后角度$ location将返回

[英]Angular $location is going back after change

I'm working with Angular and I need to redirect user to the view page after he submits the form. 我正在使用Angular,我需要在用户提交表单后将用户重定向到视图页面。

I have attached following function to scope: 我已将以下功能附加到范围:

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

This one works fine, user is redirected. 这一项工作正常,用户已重定向。

I've created a service to encapsulate the logic. 我创建了一个服务来封装逻辑。

...
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);
            }
        });
    };
};
....

So I attach it to the scope like this: 所以我将其附加到这样的范围:

$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. 提交后,用户仅“眨眼”一秒钟就被“重定向”,然后路径变回原位。 No idea what is happening. 不知道发生了什么。

You said that After submit the user is "redirected" for only a blink of a second, then the path changes back. 您说过, After submit the user is "redirected" for only a blink of a second, then the path changes back. ,so I think it should be redirect to a wrong path and then go to the otherwise branch: ,所以我认为应该将其重定向到错误的路径,然后转到else分支:

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

I suggest you recheck your customer.id if it maybe a undefined. 我建议您重新检查您的customer.id如果未定义)。

I would try to change your $scope to: 我会尝试将您的$ scope更改为:

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

Also, give a try $state.go. 另外,尝试$ state.go。 For example, let's say you have this state: 例如,假设您具有以下状态:

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

So, you could use $state.go like this: 因此,您可以像这样使用$ state.go:

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

Remember, you have to inject $state in your controller. 记住,您必须在控制器中注入$ state。

bye 再见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM