13

I have an Edit Form, where users can Update a single "auction". How can I use $state.go('auctions') correctly within the update function, so that the user gets directed to the auction-view after the update was successfull?

Or better: how can I pass parameters to this $state.go('auctions')

  // Update auction
  $scope.updateAuction = function(auction){
    auctions.updateAuction(auction, {
      product: $scope.product,
      condition: $scope.condition
    }).success(function() {
      $state.go('auctions');       // I think I need to add auction parameters here 
      ToastService.show('Auction updated');
    });
  };

The auctions $state function looks like this:

  // state for showing single Auction
  .state('auctions', {
    url: '/auctions/{product}/{id}',
    templateUrl: 'auctions/auction-view.html',
    controller: 'AuctionViewCtrl',
    resolve: {
      auction: ['$stateParams', 'auctions', function($stateParams, auctions) {
        return auctions.get($stateParams.id);
      }]
    }
  })

The Edit Form can only be accesed through the related Auction View, so maybe there is a better option to use $state.go without sending a new GET request, because the auction is already loaded?

rave
  • 1,022
  • 1
  • 12
  • 23
Alessandro Santamaria
  • 877
  • 1
  • 11
  • 25
  • 2
    `$state.go("auctions", { "id": 123});` – Razvan B. Oct 28 '15 at 13:11
  • Possible duplicate of [How to send and retrieve parameters using $state.go toParams and $stateParams?](http://stackoverflow.com/questions/19516771/how-to-send-and-retrieve-parameters-using-state-go-toparams-and-stateparams) – leocborges Jan 19 '17 at 16:39

1 Answers1

31
$state.go("auctions", {"product": auction.product, "id": auction.id}); 

Here is also a link with documentation:

https://github.com/angular-ui/ui-router/wiki/Quick-Reference#stategoto--toparams--options

rave
  • 1,022
  • 1
  • 12
  • 23
  • 1
    also the API for this http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state#methods_go – rave Oct 28 '15 at 13:24
  • But if I have `let path = `resource/${resource}/category/${category}`;` and I want to do `$state.go(path);` ? This will not work - how to handle such a thing as well? – Raz Buchnik Jul 25 '19 at 07:06