如何根据Angularjs中的值显示glyphicon眼睛?

时间:2016-11-03 05:57:11

标签: angularjs json

我对表格中的所有字段都有glyphicon-eye。如果用户点击glyphicon-eye-open,它将变为glyphicon-eye-close,并将该特定字段名称推送到数组中。

在我的JSON响应中,我获取了隐藏的字段值,但是如何使用该值并调用精确的glyphicon-eye。

JSON响应:

{
  "response": {
    "status": {
      "code": "0",
      "message": "Success"
    },
    "service": {
      "servicetype": "4",
      "functiontype": "1005"
    },
    "data": {
      "session_id": "372",
      "roles": [
        {
        "hiddenfields": [
          {
            "fname": "firstname",
            "fblink": "fblink",
            "country": "country",
            "martialStatus": "martialStatus"
          }
        ]
        }
      ]
    }
  }
}

控制器:

 $scope.user = {
            fname: "firstname",  
            lname: "lastname",
            dob: "dob",
            gender: "gender",
            country: "country",
            state: "state",
            city: "city",
            pincode: "pincode",
            martialStatus: "martialStatus",
            emailId: "emailId",
            mobile: "mobile",
            qualification: "qualification",
            fblink: "fblink"

        };

        $scope.allow = {};

        $scope.users = [];

        $scope.push = function(){
            $scope.users = [];
            var user = {}, 
                allow = $scope.allow;
            console.log(allow);
          Object.keys(allow).forEach(function(key){
            allow[key] ? user[key] = $scope.user[key] : null;
          });
          $scope.users.push(user);
        } 

HTML:

<a class="menu-toggle" class="btn btn-default" ng-model="allow.fname"><i class="glyphicon" ng-class="{'glyphicon-eye-open':allow.fname, 'glyphicon-eye-close':!allow.fname}" ng-click="push(allow.fname = allow.fname?false:true)"></i></a>

如果字段值在数组中,那么我需要显示glyphicon-eye-close。

3 个答案:

答案 0 :(得分:1)

您可以使用如下的ng-class。

<div class="form-group" ng-repeat="x in allow" >
      <button class="btn btn-default"><span class="glyphicon" ng-class="{ 'glyphicon-eye-open': x.fname==0 ,  'glyphicon-eye-close': x.fname==1}"></span> {{x.name}}</button>
 </div>

&#13;
&#13;
function myCtrl($scope) {
    $scope.allow=[{
      'fname':1,
      'name':'Anil'
    },{
      'fname':0,
      'name':'Sunil'
    },{
      'fname':1,
      'name':'Manil'
    }]
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div ng-app ng-controller="myCtrl" class="col-md-12">
    <div class="form-group" ng-repeat="x in allow" >
      <button class="btn btn-default"><span class="glyphicon" ng-class="{ 'glyphicon-eye-open': x.fname==0 ,  'glyphicon-eye-close': x.fname==1}"></span> {{x.name}}</button>
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

有时候!在ng-clss中不起作用。您可以通过

替换它
<a class="menu-toggle" class="btn btn-default" ng-model="allow.fname">
    <i class="glyphicon" 
     ng-class="{'glyphicon-eye-open':allow.fname.length > 0, 'glyphicon-eye-close':allow.fname.length == 0}" 
     ng-click="push(allow.fname = allow.fname?false:true)">
    </i>
</a>

答案 2 :(得分:0)

我现在能够通过将响应分配给我的模型来基于值进行glyphicon,如下所示

$scope.allow = response.data.roles[0].hiddenfields[0];