jeudi 27 juillet 2017

Reload ng-checked without using ng-model

I had multiple lists of ng-repeat of checkboxes, I'm currently using ng-checked and ng-click to perform checkboxes. When my backend server fetches some data and parse into the marketing.offers, the checkboxes didn't update, remain default which is all blank state.

HTML

<div ng-class="{'col-sm-offset-2': ($index % 2 == 0)}" style="padding-top:10px;" 
  ng-repeat="o in offer_options" class="col-md-4">
  <label for="" class="css-input css-checkbox css-checkbox-primary">
    <input id="" type="checkbox" name="" ng-value="o.id" 
   ng-checked="existsOffer(o, marketing.offers)" 
   ng-click="toggleOffer(o, marketing.offers)"/>
<span></span> 
  </label>
</div>

Controller

$scope.offer_options = [
   {
      id: 0,
      name: "SMS"
   },
   {
      id: 1,
      name: "Email"
   },
   {
       id: 2,
       name: "Messenger/Whatsapp/Wechat etc"
   }
];

$scope.toggleOffer = function (item, list) {
    var idx = list.map(function(e) { return e.id; }).indexOf(item.id);
    if (idx > -1) {
      list.splice(idx, 1);
    }else {
      list.push({
         id: item.id,
         name: item.name
      });
    }
};

$scope.existsOffer = function (item, list) {
    if(!isEmpty(list)){
       return list.map(function(e) { 
          return e.id; 
       }).indexOf(item.id) > -1;
     }
     return;
};




Aucun commentaire:

Enregistrer un commentaire