ng show在index.html中不工作,但在其他视图中,它在angularJs中工作良好

ng show在index.html中不工作,但在其他视图中,它在angularJs中工作良好,angularjs,Angularjs,我需要的是,带有BTN的HREF应该显示在登录/注册视图中,并且应该隐藏在dashBorad视图中 index.html <body> <div class="w3-sidebar w3-light-grey w3-bar-block" style="width:20%"> <div ng-controller="loginCtrl"> <h3 class="w3-bar-item" >Menu</h3>

我需要的是,带有BTN的HREF应该显示在登录/注册视图中,并且应该隐藏在dashBorad视图中

index.html

<body>
  <div class="w3-sidebar w3-light-grey w3-bar-block" style="width:20%">
    <div ng-controller="loginCtrl">
     <h3 class="w3-bar-item" >Menu</h3>
     <a ng-href="#!/signUp" class="w3-bar-item w3-button" ng-show="btns">SignUp</a>
     <a ng-href="#!/login" class="w3-bar-item w3-button" ng-show="btns">Login</a>
     <a ng-href="#!/addEmployee" class="w3-bar-item w3-button" ng-show="temp">Add Employee</a>
     <a ng-href="#!/userData" class="w3-bar-item w3-button" ng-show="temp">List of Employees</a>
     <a ng-href="#!/logout" class="w3-bar-item w3-button" ng-show="temp" ng-click="func()">Logout</a>
    </div>
  </div>
当我更新BTN的值时,它们在视图上仍然可见。找不到正在发生的事情。我没有使用父子视图

问题 您的问题是同一控制器的两个实例。让我们看看开始时的内容:

console.log($scope.$id);  // --> 2
但是,当我们单击登录链接时,我们用不同的id重新创建新的
loginCtrl
控制器:

console.log($scope.$id);  // --> 6
因此,我们的视图绑定到id为2的范围,但我们使用id为6的范围

  • 第二个实例<代码>$routeProvider.when。。。。控制器:'loginCtrl'

解决方案: 因为我们使用
loginCtrl
作为根控制器 从
$routeProvider

 $routeProvider
  .when('/login',{
    templateUrl:'login.html',
    //controller:'loginCtrl' // we don't need it, it will create new scope
  })



如果默认设置
$scope.btns=false,将会发生什么?如果不调用
$location.path(“/dashBoard”)?如果BTN最初为false,则不会显示登录和注册HREF。若仪表板并没有被调用,我将只在登录视图上;只是将用户重定向到仪表板。@Maxim Shoustin当用户停留在同一视图时,结果与以前相同;HREF没有隐藏。尝试在Plunker/Fiddle中重现类似的行为。我相信你在这里发布的内容存在问题,如果在app.js中我没有告诉angular使用哪个ctrl键,那么angular如何知道在我单击submit时要执行哪个login()fn。@你在HTML
中定义为根包装,angular将使用
loginCtrl
 $routeProvider
  .when('/login',{
    templateUrl:'login.html',
    //controller:'loginCtrl' // we don't need it, it will create new scope
  })