kalai kalai - 26 days ago 10
JSON Question

How to show glyphicon eye based on value in Angularjs?

I have glyphicon-eye for all fields in the form. If user click on glyphicon-eye-open then it will change to glyphicon-eye-close and I push that specific field name into an array.

In my JSON response I am getting the hidden field values but how can I use that value and call exact glyphicon-eye.

JSON response :

{
"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"
}
]
}
]
}
}
}


Controller :

$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>


If field value is in array then I need to show glyphicon-eye-close.

Answer

Some times the ! doesnot works inside ng-clss. You please replace that by

<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>
Comments