【问题标题】:Angular ui.router dynamic ui-srefAngular ui.router 动态 ui-sref
【发布时间】:2016-06-30 03:34:22
【问题描述】:

我有一个有两种状态的应用:

  • profile
  • settings

我有一个在整个应用程序中重复出现的链接:

<a ui-sref="profile({id:profile.id})" ui-sref-active="active">Current state + ID</a>

如何将ui-sref 更改为动态的? - 表示当前状态以及我想要的当前 stateParam(即id

在寻找解决方案时,请记住我希望能够使用 ui-sref-active,所以我宁愿避免 ng-click 类似这样的内容。

【问题讨论】:

标签: angularjs angular-ui-router


【解决方案1】:

我认为ui-sref 会将 ( 和 ) 中的内容解析为表达式。

所以你要做的就是这个。

<a ng-repeat="step in steps" ui-sref="{{step.state}}(step.param)"></a>

【讨论】:

【解决方案2】:

我想最安全和易于理解的方法是使用简单的if else

<div ng-if="checkState == 'profile'">
      <a ui-sref="profile({id:profile.id})" ui-sref-active="active">Current state + ID</a>
</div>

<div ng-if="checkState == 'settings'">
      <a ui-sref="settings({id:profile.id})" ui-sref-active="active">Current state + ID</a>
</div>

肯定会成功的...

【讨论】:

  • 会的,但我做不到。在这里,我有 2 个州,但在我的实际应用程序中,我有超过 2 个州。我无法创造大量条件。这是一个很好的解决方案,但不适用于我的情况。谢谢!
【解决方案3】:
$scope.routers = [
  {
    state: 'profile',
    params: {id: 1}
  },
  {
    state: 'settings',
    params: {id: 1}
  } 
];

查看:

<a ng-repeat="router in routers" ui-sref="{{router.state}}(router.params)"></a>

【讨论】:

  • {{router.state}}() -- () 括号也需要。
猜你喜欢
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 2019-09-06
  • 2014-08-12
  • 2016-06-07
  • 1970-01-01
相关资源
最近更新 更多