Improve this Doc  View Source

$$messageFormat

  1. - service in module ngMessageFormat

Angular internal service to recognize MessageFormat extensions in interpolation expressions. For more information, see: https://docs.google.com/a/google.com/document/d/1pbtW2yvtmFBikfRrJd8VAsabiFkKezmYZ_PbgdjQOVU/edit

Example

  Edit in Plunker
<div ng-controller="AppController">
  <button ng-click="decreaseRecipients()" id="decreaseRecipients">decreaseRecipients</button><br>
  <span>{{recipients.length, plural, offset:1
          =0    {{{sender.name}} gave no gifts (\#=#)}
          =1    {{{sender.name}} gave one gift to {{recipients[0].name}} (\#=#)}
          one   {{{sender.name}} gave {{recipients[0].name}} and one other person a gift (\#=#)}
          other {{{sender.name}} gave {{recipients[0].name}} and # other people a gift (\#=#)}
        }}</span>
</div>
function Person(name, gender) {
  this.name = name;
  this.gender = gender;
}

var alice   = new Person("Alice", "female"),
    bob     = new Person("Bob", "male"),
    charlie = new Person("Charlie", "male"),
    harry   = new Person("Harry Potter", "male");

angular.module('msgFmtExample', ['ngMessageFormat'])
  .controller('AppController', ['$scope', function($scope) {
      $scope.recipients = [alice, bob, charlie];
      $scope.sender = harry;
      $scope.decreaseRecipients = function() {
        --$scope.recipients.length;
      };
    }]);
describe('MessageFormat plural', function() {
  it('should pluralize initial values', function() {
    var messageElem = element(by.binding('recipients.length')), decreaseRecipientsBtn = element(by.id('decreaseRecipients'));
    expect(messageElem.getText()).toEqual('Harry Potter gave Alice and 2 other people a gift (#=2)');
    decreaseRecipientsBtn.click();
    expect(messageElem.getText()).toEqual('Harry Potter gave Alice and one other person a gift (#=1)');
    decreaseRecipientsBtn.click();
    expect(messageElem.getText()).toEqual('Harry Potter gave one gift to Alice (#=0)');
    decreaseRecipientsBtn.click();
    expect(messageElem.getText()).toEqual('Harry Potter gave no gifts (#=-1)');
  });
});