vendredi 7 juillet 2017

Passing parameters in Angular js Getters

I am using angular 1.6.x

I have a list of checkboxes which maintain their state using a variable (selectedJobIds). The variable contains selected job ids, like,

{
 23: true,
 56: true,
 47: false
}

Now, I need to checked the checkboxes which are existing in this object and have value true. Also, the checkboxes should be checked if they don't exist in the object.

Right now, its not able differentiate between non-existent id and id with false value. It unchecks both of them.

Tried Approaches: 1) I tried to implement ternary condition in ng-model="(angular.isDefined(selectedJobIds[job.iid])?selectedJobIds[job.iid]:true)"

But it throws syntax error.

2) I tried using getter/setter of ng-model-options But the problem is that I can't able to pass job id to the getter function. Also, I am not able to get the scope of the current element, so I can access its value. So, I can use this id to detect its existence and non-existence in selectedJobIds variable.

HTML:

<ul class="list-unstyled filtered-trucks-ul">
  <li ng-repeat="job in dType track by $index" style="vertical-align: middle;">
      <input type="checkbox" name="job_id" value="" ng-click="selectFilterJob()" ng-model="selectedJobIds[job.iid]">
      <img src="23.png">
      
      </li>
</ul>

JS:

$scope.checkedFilteredJobs = {
         getterFunc: function(newName) {
            console.log(newName)
            // if(angular.isDefined($scope.selectedJobIds[id]))
            // {
            //    return $scope.selectedJobIds[id];
            // }
            // else {
               return true;
            // }
         }

};

Aucun commentaire:

Enregistrer un commentaire