javascript - 更新 ng-if 变量后,Angular 显示空屏

标签 javascript angularjs

我使用一个整数来表示我的应用程序中的状态,其中我有几个(3)容器使用 <div ng-if="state == value">显示该页面的正确信息。问题是当 state变化,屏幕变白。没有错误,什么也没有。只是空白。

angular.module('CharCount')

  .controller('MainCtrl', function($scope, $timeout) {

    $scope.state = 0;
    
    $scope.stateText = "waiting";
    
    $scope.clicked = function() {
      $timeout(function() {
        $scope.state = 1;
        $scope.state = "loading";
        $scope.test(null, function() {
          $scope.state = 2;
          $scope.state = "finished, should show State C";
        });
      });
    };
    
    $scope.test = function(a, callback) {
      $timeout(callback);
    };
    

  });
<html>

  <head>
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
  </head>

  <body ng-app="CharCount">
    <div ng-controller="MainCtrl">
      <div ng-if="state == 0">
        State A<br>
        {{stateText}}<br>
        <button ng-click="clicked()">click me</button>
      </div>
      
      <div ng-if="state == 1">
        State B<br>
        {{stateText}}
      </div>
      
      <div ng-if="state == 2">
        State C<br>
        {{stateText}}
      </div>
    </div>
    <script src="angular-character-count.js"></script>
    <script src="app.js"></script>
  </body>

</html>

如果有人能解释为什么会发生这种情况,那会有很大帮助。

最佳答案

代码中存在很多问题:

  1. 要使用 getter 获取应用实例,请将第二个参数作为空数组传递给 module()。 ==> angular.module('CharCount', [])
  2. $timeout 回调中,变量 state 被分配了一个字符串。

    $scope.state = 1;
    $scope.state = "loading"; // This is incorrect
    

    由于 View 中没有条件,并且所有其他条件都评估为 false,因此不会显示任何元素。

  3. 逻辑错误:应在调用 $timeout 之前设置值。

    $scope.state = 1;
    $scope.stateText = "loading";
    

    将这些语句移至 $timeout 之上。

演示:

angular.module('CharCount', [])                      // <--- #1

.controller('MainCtrl', function($scope, $timeout) {

  $scope.state = 0;
  $scope.stateText = "waiting";

  $scope.clicked = function() {
    $scope.state = 1;                                // <--- #3
    $scope.stateText = "loading";                    // <--- #2

    $timeout(function() {
      $scope.test(null, function() {
        $scope.state = 2;
        $scope.stateText = "finished, should show State C"; // <--- #2
      });
    }, 1000);
  };

  $scope.test = function(a, callback) {
    $timeout(callback);
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>

<body ng-app="CharCount">
  <div ng-controller="MainCtrl">
    <div ng-if="state == 0">
      State A
      <br>{{stateText}}
      <br>
      <button ng-click="clicked()">click me</button>
    </div>

    <div ng-if="state == 1">
      State B
      <br>{{stateText}}
    </div>

    <div ng-if="state == 2">
      State C
      <br>{{stateText}}
    </div>
  </div>

</body>

关于javascript - 更新 ng-if 变量后,Angular 显示空屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36613898/

相关文章:

javascript - 打开一个特定变量 Angular JS

javascript - 如何获取非javascript 'script' 的内容?

css - angularjs ng-class 无法与 Bootstrap 一起正常工作

php - audio.js 音频播放器不适用于 angularjs ng-src

javascript - 如何保存自动生成字段中的数据并将其与单个 ID 关联?

javascript - Angularjs Restangular 未获取数据

javascript - 单击 html 按钮时使用 javascript 将 json 数据发送到托管的 python Flask api,并在 HTML 标签上打印 API 的响应

javascript - 确定一个元素的offsetTop并将其设置在其他元素上

javascript - AngularJS $locationProvider.html5Mode(true) 给出错误 `url is undefined`

javascript - AngularJS 中的树状复选框