无法使用ng-hide指令隐藏/显示index.html中的导航栏

时间:2015-08-17 21:14:54

标签: javascript html angularjs

我正在编写我的第一个小型网络应用程序,即CMS。

无论如何,一旦用户登录,我希望能够通过操纵ng-hide指令隐藏或显示index.html中的导航栏。

到目前为止,这是我的代码。

的index.html

<!doctype html>
<html>
  <head>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    <meta charset="utf-8">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
    <!-- build:css(.) styles/vendor.css -->
    <!-- bower:css -->
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
    <!-- endbower -->
    <!-- endbuild -->
    <!-- build:css(.tmp) styles/main.css -->
    <link rel="stylesheet" href="styles/main.css">
    <link rel="stylesheet" href="styles/app.css">
    <!-- endbuild -->
  </head>
  <body ng-app="WebAppApp">
    <!--[if lte IE 8]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

    <!-- Navigation -->
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation" ng-hide="menuHide">
        <div class="container">
      <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Start Bootstrap</a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li>
                        <a href="#">About</a>
                    </li>
                    <li>
                        <a href="#">Services</a>
                    </li>
                    <li>
                        <a href="#">Contact</a>
                    </li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
    </nav>

    <div ng-view=""></div>

    <!-- Google Analytics: change UA-XXXXX-X to be your site's ID -->
     <script>
       !function(A,n,g,u,l,a,r){A.GoogleAnalyticsObject=l,A[l]=A[l]||function(){
       (A[l].q=A[l].q||[]).push(arguments)},A[l].l=+new Date,a=n.createElement(g),
       r=n.getElementsByTagName(g)[0],a.src=u,r.parentNode.insertBefore(a,r)
       }(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

       ga('create', 'UA-XXXXX-X');
       ga('send', 'pageview');
    </script>

    <!-- build:js(.) scripts/vendor.js -->
    <!-- bower:js -->
    <script src="bower_components/jquery/dist/jquery.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
    <script src="bower_components/angular-animate/angular-animate.js"></script>
    <script src="bower_components/angular-aria/angular-aria.js"></script>
    <script src="bower_components/angular-cookies/angular-cookies.js"></script>
    <script src="bower_components/angular-messages/angular-messages.js"></script>
    <script src="bower_components/angular-resource/angular-resource.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
    <script src="bower_components/angular-touch/angular-touch.js"></script>
    <!-- endbower -->
    <!-- endbuild -->

        <!-- build:js({.tmp,app}) scripts/scripts.js -->
        <script src="scripts/app.js"></script>
        <script src="scripts/controllers/login.js"></script>
        <script src="scripts/controllers/signup.js"></script>
        <script src="scripts/controllers/home.js"></script>
        <script src="scripts/services/apiService.js"></script>
        <script src="scripts/services/loggedinService.js"></script>
        <!-- endbuild -->
</body>
</html>

我在index.html

中的nav标签中定义了ng-hide指令
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation" ng-hide="menuShowHide">

控制器

home.js

'use strict';

app.controller('homeCtrl', function($scope, $http, $cookies, $rootScope, $location, loggedInStatus, apiServiceWeb) {

//unhide menu by accessing ng-hide variable set in index.html
  $scope.menuHide = false;

});

在控制器中,我现在希望能够通过以下方式为导航设置true或false:

$scope.menuHide = false;

$scope.menuHide = true;

但它似乎并没有起作用。知道为什么会发生这种情况以及如何使这项工作成功吗?

由于

2 个答案:

答案 0 :(得分:0)

创建一个jsFiddle示例。

  1. 你打电话给homeCtrl吗?例如这样 <div ng-controller="homeCtrl"> <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation" ng-hide="menuHide"> </div>

  2. 在控制器中创建断点并检查它是否正在创建。

答案 1 :(得分:0)

您似乎正在尝试设置homeCtrl,而homeCtrl的范围不是menuShowHide内的$rootScope。解决此问题的最佳方法是创建一个新的控制器来处理菜单show hide,但如果您尝试设置其他控制器的可见性,则需要使用AngularJS Services/Factories

快速而肮脏的解决方法是使用$scope代替app.controller('homeCtrl', function($scope, $http, $cookies, $rootScope, $location, loggedInStatus, apiServiceWeb) { //unhide menu by accessing ng-hide variable set in index.html $rootScope.menuShowHide = false; }); 设置Sheets("Sheet1").cells.copy Sheets("Sheet1").cells.PasteSpecial Paste:=xlPasteValues

Sub Test1()

    Dim WS_Count As Integer
    Dim I As Integer


    WS_Count = ActiveWorkbook.Worksheets.Count

    For I = 1 To WS_Count

        ActiveWorkbook.Worksheets(I).Cells.Copy
        ActiveWorkbook.Worksheets(I).Cells.PasteSpecial Paste:=xlPasteValues

    Next I

End Sub