https://stackoverflow.com/questions/44523681

https://stackoverflow.com/questions/44523681

by Daan De Smedt

HTML

<div ng-app="app" ng-controller="MainController as vm">
  <div ng-click="vm.setShops()">
    Set shops
  </div>
  <div>
    {{vm.shops}}
  </div>
</div>

JavaScript

angular
  .module('app', [])
  .controller('MainController', MainController)
  .service('ecommercer', ecommercer)

function ecommercer() {
  var shops;

  return {
    setShops: setShops,
    getShops: getShops
  }

  function setShops(shops) {
    this.shops = shops;
  }

  function getShops(shops) {
    return this.shops;
  }

}


function MainController(ecommercer) {
  var vm = this;
  this.setShops = setShops;

  function setShops() {
    ecommercer.setShops([{
      id: 1,
      name: 'test shop 1'
    }, {
      id: 2,
      name: 'test shop 2'
    }]);
    vm.shops = ecommercer.getShops();
  }

}