Angular, ui-router:如何从指令模板中获取ngShow

Angular, ui-router: how to ngShow from Directive template?

本文关键字:获取 ngShow 指令 ui-router Angular      更新时间:2023-09-26

我使用的代码是这样的:

https://github.com/tarlepp/angular-sailsjs-boilerplate/blob/master/frontend/src/app/angular/Directives/NavigationHeader.js

和我的模板代码是这样的:

https://github.com/tarlepp/angular-sailsjs-boilerplate/blob/master/frontend/src/app/partials/Directives/NavigationHeader/header.html

:

如果我的ui-router的状态是" anonymous .about",如何从"header.html"中"ngShow"一个"div" ?

如果你需要在你的模板中检查状态,你可以这样做:

ng-show="current == $state.includes('<Your State Name>')"

并将current作为作用域变量传递给当前处于哪个状态的指令。这可以通过注入$stateParams依赖项来实现。

更新代码:

In header.html

<div class="">
  <div data-ng-show="$root.currentStateName=='anon.about'">Test!</div>
  <div>{{$root.currentStateName}}</div>
</div>
在app.js:

.state('anon.about', {
            url: '/about',
            templateUrl: 'about.html',
            controller: function($rootScope,$state){
              console.log($state.current.name);
              $rootScope.currentStateName = $state.current.name;
            }
})