如何在$ state.go中添加参数?

时间:2015-10-28 13:03:43

标签: angularjs angular-ui-router

我有一个编辑表单,用户可以在其中更新单个“拍卖”。如何在更新功能中正确使用$ state.go('auctions'),以便在更新成功后用户被定向到拍卖视图?

或更好:我如何将参数传递给此$ state.go('auction')

  // 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');
    });
  };

拍卖$ state函数如下所示:

  // 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);
      }]
    }
  })

编辑表格只能通过相关的拍卖视图加入,所以也许有更好的选择使用$ state.go而不发送新的GET请求,因为拍卖已经加载了?

1 个答案:

答案 0 :(得分:25)

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

这里还有一个文档链接:

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