【问题标题】:ng-hide and ng-show in ng-repeat html table angularjsng-repeat html 表 angularjs 中的 ng-hide 和 ng-show
【发布时间】:2015-05-09 16:46:05
【问题描述】:

我的表格在使用 ng-repeat 生成的 html 视图中。在表格中,当我单击某些 ID 行中的“显示检查”按钮(例如 1)时,“检查​​”字应该在 ID 号 1 旁边可见,当我单击 2 时,“检查​​”字应该在 ID 号 2 旁边可见并且数字 1 中的“CHECK”一词应该被删除或不可见。我在表中使用 ng-repeat。它几乎工作正常,除了当我单击按钮 2 次时,另一个“检查”字仍然可见,这不应该。任何帮助将非常感激。谢谢

这是我的小提琴:http://jsfiddle.net/DharkRoses/onao6ddn/

示例代码:

 <tr ng-repeat="user in users">
        <td><p ng-show ="user.myVar">CHECK</p>{{user.id}}</td>
        <td>{{user.name}}</td>
        <td>{{user.stat}}</td>
        <td><button id ="btn" ng-click = "toggleShow(user)">show check</button></td>
 </tr>

【问题讨论】:

  • 你有什么问题?
  • 嗨@Rebornix,我现在的问题是我想显示一次“CHECK”字样。

标签: html angularjs


【解决方案1】:

只需将myVar 的所有其他值设置为false:

angular.forEach($scope.users, function(value, key) {
      value.myVar = false; 
});

我已更新fiddler

【讨论】:

    【解决方案2】:

    使用 $index Fiddle

    $scope.toggleShow = function(user,$index){
        for(var i=0;i<$scope.users.length;i++){
            $scope.users[i].myVar='false'
        }
        $scope.users[$index].myVar='true'
    };
    
    
       <td><button id ="btn" ng-click = "toggleShow(user,$index)">show check</button></td>
    
    Users data little modified :-
     $scope.users = [ 
           {
              id:1,
              name:'Mary Land',
              stat:51,
              myVar:'false'
            },
            {
              id:2,
              name:'Kenhie Land',
              stat:51,
                myVar:'false'
            },
            {
              id:3,
              name:'Mary Land',
              stat:51,
                myVar:'false'
            },
            {
              id:4,
              name:'Kenhie Land',
              stat:51,
                myVar:'false'
            },
            {
              id:5,
              name:'Mary Land',
              stat:51,
                myVar:'false'
            },
            {
              id:6,
              name:'Kenhie Land',
              stat:51,
                myVar:'false' 
            },
            {
              id:7,
              name:'Mary Land',
              stat:51,
                 myVar:'false' 
            },
            {
              id:8,
              name:'Kenhie Land',
              stat:51,
                 myVar:'false' 
            },
            {
              id:9,
              name:'Mary Land',
              stat:51,
                 myVar:'false' 
            },
    
    
           ];
    

    【讨论】:

    • 为每个用户添加默认 false 然后迭代 b/w 用户以根据选择为他们赋予价值。检查更新的小提琴
    【解决方案3】:

    简单的解决方案:

    在切换 myvar 时备份用户,然后使用它。

     previousUser={};
    $scope.myVar = true;
    $scope.toggleShow = function(user){
        previousUser.myVar=false;
        previousUser=user;
      user.myVar =!user.myVar;
    
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-08
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 2015-06-29
      相关资源
      最近更新 更多