美文网首页
循环输出时的$scope

循环输出时的$scope

作者: 月亮是我啃的 | 来源:发表于2017-08-08 10:49 被阅读0次

问题描述:

今天老大让改项目中的bug,我发现ng-repeat循环输出时,利用ng-if显示数据结果总是一致的,后台输出完全没有问题,显示的结果与后台打印的不一致,看了半天才发现是$scope的问题


修改前.png

1-1、修改前html页面代码(局部):

 <li ng-repeat="item in data">
         名称:<h3>{{item.Name}}</h3>
         <div ng-if="isAttendNeed">
             <button type="button" class="btn btn-info"><span style="font-size:larger;">报 名</span></button>
         </div>

         <div ng-if="isAttendAll">
             <button type="button" class="btn btn-default"><span style="font-size:larger;">已报名</span></button>
          </div>

          <div ng-if="isUnAttendAll">
              <button type="button" class="btn btn-default"><span style="font-size:larger;">已 满</span></button>
          </div>
 <li ng-repeat="item in data">

1-2、修改前部分js代码:

 if (response) {
     console.log(item);
     $scope.isDisabled = true;
     $scope.isAttendAll = true;//已报名
     $scope.isAttendNeed = false;
     $scope.isUnAttendAll = false;
 }else {
        if ($scope.IsFullTrain) {
            $scope.isUnAttendAll = true;//未参加,人数已满(已满)
            $scope.isAttendNeed = false;
            $scope.isAttendAll = false;
        } else {
            $scope.isAttendNeed = true;//报名
            $scope.isAttendAll = false;
            $scope.isUnAttendAll = false;
        }
  }
            console.log($scope.isAttendNeed + "报名");
            console.log($scope.isAttendAll + "已报名");
            console.log($scope.isUnAttendAll + "已满");
说明:

$scope是全局变量,所以循环输出数据时,$scope.isAttendNeed、$scope.isAttendAll、$scope.isUnAttendAll三个属性的值等于最后一次所赋的值,所以循环出所有数据显示的结果是一致的。

解决方法:

将这三个属性设置成局部变量即可,将$scope换成item,在html页面中利用item.属性即可正常显示。

修改后.png

2-1、修改后html页面代码(局部):

 <li ng-repeat="item in data">
         名称:<h3>{{item.Name}}</h3>
         <div ng-if="item.isAttendNeed">
             <button type="button" class="btn btn-info"><span style="font-size:larger;">报 名</span></button>
         </div>

         <div ng-if="item.isAttendAll">
             <button type="button" class="btn btn-default"><span style="font-size:larger;">已报名</span></button>
          </div>

          <div ng-if="item.isUnAttendAll">
              <button type="button" class="btn btn-default"><span style="font-size:larger;">已 满</span></button>
          </div>
 <li ng-repeat="item in data">

2-2、修改后部分js代码:

 if (response) {
     console.log(item);
     item.isDisabled = true;
     item.isAttendAll = true;//已报名
     item.isAttendNeed = false;
     item.isUnAttendAll = false;
 }else {
        if (item.IsFullTrain) {
            item.isUnAttendAll = true;//未参加,人数已满(已满)
            item.isAttendNeed = false;
            item.isAttendAll = false;
        } else {
            item.isAttendNeed = true;//报名
            item.isAttendAll = false;
            item.isUnAttendAll = false;
        }
  }
            console.log(item.isAttendNeed + "报名");
            console.log(item.isAttendAll + "已报名");
            console.log(item.isUnAttendAll + "已满");

相关文章

  • 循环输出时的$scope

    问题描述: 今天老大让改项目中的bug,我发现ng-repeat循环输出时,利用ng-if显示数据结果总是一致的,...

  • 2018-01-07 执行上下文相关

    scope 函数在创建时,会添加[[scope]]属性 parent的[[scope]] = [globalCon...

  • vue中el-table的简单使用

    情景一 作用域插槽slot-scope="scope"循环判断 过滤: 比如传过来的数组是numbers要过滤掉不...

  • name_scope与variable_scope的区别

    name_scope仅仅作用于操作上,并且不影响variable_scope。这么说有点抽象,如下代码: 输出: ...

  • swift day2

    for in循环 Nested function 嵌套函数 Closure scope用来定义编程语言中的各种实体...

  • 【编程】python入门---while循环01

    while循环 语法: Ctrl + C: 强制结束死循环 for 循环:输出1到10 一、利用while循环输出...

  • maven随笔

    1、dependence scope【默认为compile范围,】 当scope为compile时,会在编译的时候...

  • 基础

    输入 输出 循环 简单数组循环

  • Break Continue Return

    1.break 打印输出 当执行break时,会结束循环. 2.continue 打印输出 当执行continue...

  • Shell使用循环

    递增递减循环 循环输出数组内容

网友评论

      本文标题:循环输出时的$scope

      本文链接:https://www.haomeiwen.com/subject/itftrxtx.html