在$ stateParams中传递多个参数

时间:2017-03-22 10:12:57

标签: angularjs angular-ui-router

在ui-router教程https://plnkr.co/edit/jbZgIg?p=info中,他们将一个参数 personId 传递给该状态。

ui-sref="people.person({ personId: person.id })"

状态是:

{ 
  name: 'people.person', 
  url: '/{personId}', 
  component: 'person',
  resolve: {
    person: function(people, $stateParams) {
      return people.find(function(person) { 
        return person.id === $stateParams.personId;
      });
    }
  }
}

我正在研究一个示例,其中我想传递2个参数(1个使用url可见,第二个隐藏,这将是用户无法在url中看到的id)。类似于:

ui-sref="people.person({ personId: person.id, personUri : person.uri })"

我希望国家成为这样的东西(但它没有用!):

{ 
  name: 'people.person', 
  url: '/{personUri}',
  component: 'person',
  resolve: {
    person: function(people, $stateParams) {
      return people.find(function(person) { 
        return person.id === $stateParams.personId;
      });
    }
  }
}

如州所示,我想在网址中使用 personUri ,我希望将 personId 传递给服务。

2 个答案:

答案 0 :(得分:1)

你可以使用params

.state('other', {
    url: '/:personUri',
    params: { 
        personUri:"",

        // this param is not part of url
        // it could be passed with $state.go or ui-sref 
        personId: "",
      }
...

答案 1 :(得分:0)

不是在url参数中传递对象,而是通过传入对象来调用控制器的函数,然后在控制器本身中对该对象执行任何操作,然后在其中调用服务。

示例:

$scope.func = function info(person){
    console.log(person);
    PeopleService.xyz(person).then(function(response){
        console.log('Service called')
    })
  }

在html中称之为:

<button ng-click="func(person)"> Click me</button>
相关问题