angular-local-storage icon indicating copy to clipboard operation
angular-local-storage copied to clipboard

Using bind with controllers that use controllerAs syntax

Open chrissearle opened this issue 10 years ago • 8 comments

Not sure what to send in as scope when using controllers that are set up with controllerAs syntax (that is with all properties etc on "this"). Any pointers?

chrissearle avatar Nov 12 '14 10:11 chrissearle

+1

bluedaniel avatar Dec 11 '14 13:12 bluedaniel

+1

jacekd avatar Feb 13 '15 18:02 jacekd

+1

GabrielGil avatar Feb 23 '15 13:02 GabrielGil

+1

jouke avatar Mar 16 '15 19:03 jouke

Hello y'all!! That's another practical example done with a localstorage implementation done "the right way" for angular. Exactly with the binding you are talking about but done properly. Here a working example with a "Cart" and "Product" controllers and a CartService for holding the data. Within the controller would be all right as well. Give it a try... It even synchronizes between open tabs. http://codepen.io/GabrielGil/pen/VYExZv

GabrielGil avatar Mar 16 '15 22:03 GabrielGil

+1

levivm avatar Mar 20 '15 17:03 levivm

+1

patlux avatar Apr 23 '15 09:04 patlux

My solution: Given a { model: { id: 1, name: 'foo' } } structure:

angular.controller('StoreSearchController', function($scope, localStorageService) {
  var vm = this;

  var localStorageKey = 'store';
  vm.model = localStorageService.get(localStorageKey) || {};
  localStorageService.bind($scope, 'vm', vm.model, localStorageKey);

  $scope.$watchCollection(function () {
    return vm.model;
  }, function(model) {
    // Bind the view model's value to the $scope, 
    // so the storage service can catch it.   
    $scope.vm = model;
  });
});

Tested on version 0.2.0.

rebelliard avatar May 18 '15 03:05 rebelliard