简体   繁体   中英

routes redirect in angularJS

So I have got the usual routing thing, but now i would like to ask if i can redirect to a different page, but before this happens I would like to call a function.

Thats what i have so far. but its not working properly.

when('/person/remove/:Id', {
    resolve:  {
        delay: function ($q, $route, person) {
            var currentIndex = $route.current.params.Id;
            var brochure = person.get(currentIndex);
            person.remove(brochure);
        }
     },
     redirectTo: '/person'
})

Are you sure that you should be using a route for this? I take it from your example that the remove url should never display anything, it should just call the server and then redirect? If that's the case then you shouldn't be using a url to delete a record. A click in your GUI would trigger a service to delete the record in the background, and redirect when that is done.

So instead of going from /person/123 to /person/123/remove and then /person, you'd just go from /person/123 to /person. The removal is an action and shouldn't represented by it's own URL in your application.

Well, if do you something like this ...:

when('/person/remove/:Id', {
  resolve:  {
    delay: function ($q, $route, person, $location) {
      var currentIndex = $route.current.params.Id;
      var brochure = person.get(currentIndex);
      person.remove(brochure)
         .then(function(){ $location.path('/person'); });
    }
   }
})

... it will work, but it's pretty smelly and not in "Angular way'. I mean, you should probably not use angular routes to do this kind of stuff for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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