范围更改时,角度逻辑表达式不会更新

时间:2016-11-17 03:36:08

标签: javascript jquery angularjs

为什么当内部范围发生变化时,角度逻辑表达式不会更新值?

我有一个指令ng-show,当逻辑为真时显示div:

<div ng-show="!employee.name && !employee.job"></div>

比如说:

$scope.employee.name = true;
$scope.employee.job = false;

会隐藏div(!employee.name&amp;&amp;!employee.job == false)。但是,当我们使用角度$http动态更改员工时:

$http.get('/api/job').
success(function(data) {
    $scope.employee.name = data.name; // false
    $scope.employee.job = data.job; // false
}); // !employee.name && !employee.job == true

视图中的逻辑值不会改变:

<td><% employee.name %></td> <!-- false-->
<td><% employee.job %></td> <!-- false-->
<!-- !false && !false == true -->
<td><% !employee.name && !employee.job %></td> <!-- STILL FALSE -->
<!-- funny thing is no error occurred -->

这就是div为什么 NOT 显示成功$http request,因为逻辑确实更新?

<div ng-show="!employee.name && !employee.job"></div> <!-- does not show -->

1 个答案:

答案 0 :(得分:0)

如果以下示例,请检查您的值,如果更改

var emp= {
 name:undefined,
  job:false
 }

然后ng-show保持原状

&#13;
&#13;
var app = angular.module("myapp", []);
app.controller("myCtrl", ['$scope', function($scope) {
  
  var emp= {
  name:false,
  job:false
  }
   
  
  $scope.employee = emp;

  
}]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">  
  <div ng-controller="myCtrl">    
  Name: {{employee.name}}   <br/>
    Job: {{employee.job}}   <br/>
    <br/>
    Condition Status : {{!employee.name && !employee.job}}
    
    <div ng-show="!employee.name && !employee.job">
      <h1>Div </h1>
    </div>
    
  </div>
</div>
&#13;
&#13;
&#13;