AngularJS + UI 路由器 : See if href or ui-sref matches a state

标签 angularjs angular-ui-router

所以目前,我正在使用 UI-Router 来管理一组选项卡指令。通过选项卡,我的意思是这个 UI 范式:http://www.hollance.com/wp-content/uploads/2011/11/Screenshot.png

每个标签都可以有一个 title , 一个 icon , 和 hrefui-sref与之相关。

例子:

<my-tabs>
  <my-tab title="Tab 1" href="#/tab1">
    Content when tab 1 is active!
  </my-tab>
  <my-tab title="Tab 2" ui-sref="tabs.numberTwo()">
    Content when tab 2 is active!
  </my-tab>
</my-tabs>

无论如何,主要问题是,当以编程方式或通过浏览器刷新更改状态时,我希望能够基于 ui-sref 知道要选择哪个选项卡。或 href属性。 $state.(is|includes|contains)似乎没有做我需要的:我不能给 $state.is href并询问它是否与当前状态匹配 - 我也不能给它一个 ui-sref。

我可以添加一个 stateName属性或类似的选项卡,但如果我不需要,我宁愿不这样做。

基本上,我希望能够在 tab 指令中做到这一点:
$rootScope.$on('$stateChangeSuccess', function() {
   if ($state.matchesHref(attrs.href) || $state.matchesSref(attrs.uiSref)) {
    selectThisTab();
  }
});

任何想法都可以做到这一点!

最佳答案

结帐下一个方法:

http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state

  • is()
  • contains()
  • includes()
  • 关于AngularJS + UI 路由器 : See if href or ui-sref matches a state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22558800/

    相关文章:

    angularjs - 带有 typescript 的angular-ui-router嵌套命名 View

    javascript - $stateChangeStart 在 UI-Router ES6 中的状态变化时没有被触发?

    angularjs - 使用 AngularJS 限制字符串的长度

    angularjs - 用@ 定义的隔离作用域属性在指令的链接函数中未定义/消失

    javascript - 找不到 ng-click 函数中的参数

    angularjs - 使用 ng-show 指令切换多个元素?

    javascript - 为什么 ng-bind-html 只显示文本而不显示链接?

    javascript - Angular UI 路由器 : Nested Views Not Working

    javascript - 如何使用 Angular ui 路由器 ng-hide 和 ng-show View ?

    javascript - 检查父状态 Promise 是否已返回值