【问题标题】:Angular, ui-router: how to ngShow from Directive template?Angular,ui-router:如何从指令模板进行 ngShow?
【发布时间】:2014-07-09 21:17:51
【问题描述】:

我正在使用这样的代码:

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 的状态为“anon.about”时,如何从“header.html”“ngShow”一个“div”?

【问题讨论】:

    标签: javascript angularjs angular-ui-router sails.js


    【解决方案1】:

    如果您需要检查模板中的状态,您可以执行以下操作:

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

    并将当前作为范围变量传递,就像当前具有指令的状态一样。这可以通过注入 $stateParams 依赖来完成。

    更新代码:

    在 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;
                }
    })
    

    【讨论】:

    • 为您的问题创建一个小提琴或更好的 plunker,然后最好在其中编写代码并给出适当的解决方案
    • 我无法为你创建一个 plunker。但是,我的代码与我在问题中发布的代码相同。你可以在你的电脑上用这个代码试试吗?
    • 请帮帮我。说真的,我在哭。
    • 会拿出一个笨蛋别着急
    • 您好,我检查了您的代码,您想在哪里进行 ng-show?
    猜你喜欢
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    相关资源
    最近更新 更多