Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-app="myApp">
  <div ng-controller="myController">
    <p>I want to be able to access the partner as 'PartnersService.partner' and see as it changes.</p>
    <p>{{PartnersService.partner}}</p>
    
    <p>I would like to avoid using getters to correctly see our current partner's name:</p>
    <p> ({{PartnersService.getPartner()}}).</p>
    
    <p>I also solved the binding by adding an extra object literal: {{PartnersService.extraObjectLiteral.partner.name}}, however this still results in a code that doesn't feel natural.</p>
    
    <p>Neither of these solutions seem to me angularish and I am afraid I'm missing something obvious.</p>
  </div>
</body>
</html>
 
angular.module('myApp', []);
angular.module('myApp').controller('myController', function ($scope, $log, PartnersService) {
  
  $scope.PartnersService = PartnersService; // just stick it to the scope
  $log.log("should be default partner: " + PartnersService.partner.name);
  
  PartnersService.initPartner();
  $log.log("After initPartner, the current partner is still the " + PartnersService.partner.name + " but I expected " + PartnersService.partners.secondPartner.name);
  $log.log('I could use a getter: ' + PartnersService.getPartner().name);
  
  $log.log('or nest the partner in an extra object literal: ' + PartnersService.extraObjectLiteral.partner.name);
  
  $log.log("Am I still stuck with default partner? " + ("Default Partner" == PartnersService.partner.name));
});
angular.module('myApp').factory('PartnersService', function ($location, $log) {
  
  // some predefined partners that I can choose from. Context: Example could be a beverage store website, that changes as the current partner, or referral is recognized. In this case, partners could be  Coca Cola, Heineken, and the default would be the beverage store's design and branding.
  var partners = {
    firstPartner: {
      name: 'Default Partner',
      id: 1 // just an extra property as example 
    },
    secondPartner: {
      name: 'Other Partner',
      id: 2
    }
  };
  
  // set default value
  var partner = partners.firstPartner;
    
  var extraObjectLiteral = {
    partner: partner
  };
  
  var initPartner = function () {
    // based on some logic (omitted), select partner
    partner = partners.secondPartner;
    $log.log("initPartner should have changed partner to " +  partner.name);
    // this looks like a workaround for me
    extraObjectLiteral.partner = partners.secondPartner;
  };
  
  var getPartner = function () {
    return partner;
  };
  
  return {
    initPartner: initPartner,
    getPartner: getPartner,
    partners: partners,
    partner: partner,
    extraObjectLiteral: extraObjectLiteral
  };
  
  
  
  
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers