mardi 23 février 2016

checkbox checked persistent with local Storage in angular js

I want to make "checkbox are persistent" with localStorage when I reload my view are not lost.

The values that I'm saving the checked in localStorage with the help of ngStorage.

This is my code and demo http://ift.tt/1KGiEb0

<div ng-app="myApp">
   <div ng-controller="favCtrl">


    <div ng-repeat="chip in colores" >
        <input type="checkbox" name="{{chip.codigo}}" id="{{chip.codigo}}" ng-model="chip.checked" ng-change="chipsColores()"  ng-click="$storage.a = fav">
        <label>{{chip.codigo}}</label>
      </div>

        <div ng-repeat="favorite in $storage.a">
                         localStorage: {{favorite.codigo}}
                 </div>

    </div>
</div>

my controller:

(function(){

    angular.module('myApp',['ngStorage'])

  .controller('favCtrl',[ '$scope', '$filter', '$localStorage', function ( $scope, $filter, $localStorage) {
        $scope.colores = [
    {'nombre':'blue', 'codigo':'1111'},
    {'nombre':'green', 'codigo':'2222'},
    {'nombre':'red', 'codigo':'3333'}

  ];

    $scope.chipsColores = function () {
        $scope.fav = $filter('filter')($scope.colores, {checked: true});
}
  $scope.$storage =  $localStorage.$default({

});


}])
})();

Please I need your help in my demo! Thanks...




Aucun commentaire:

Enregistrer un commentaire