Tutorial 24.1 Basic Example

******************************************INDEX.HTML*****************************************************
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-message-format-example-production</title>
 

  <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
  <script src="//code.angularjs.org/snapshot/angular-message-format.js"></script>
  <script src="app.js"></script>
 

 
</head>
<body ng-app="messageFormatExample">
  <div ng-controller="ckCtrl">
  <b>Set number of recipients</b>
  <button ng-click="setNumRecipients(0)">None</button>
  <button ng-click="setNumRecipients(1)">One</button>
  <button ng-click="setNumRecipients(2)">Two</button>
  <button ng-click="setNumRecipients(3)">Three</button>


  <br><br>
  <b>Sender's</b> name: <input ng-model="sender.name"> &nbsp;&nbsp;

  <br><br><b>Recipients</b><br>
  <div ng-repeat="recipient in recipients">
    Name: <input ng-model="recipient.name"> &nbsp;&nbsp;
    Gender: <button ng-click="setGender(recipient, 'male')">male</button>
            <button ng-click="setGender(recipient, 'female')">female</button>
            <button ng-click="setGender(recipient, 'other')">other</button>
  </div>

  <br><br><b>Message</b><br>
  {{recipients.length, plural, offset:1
      =0 {You ({{sender.name}}) gave no gifts}
      =1 { {{ recipients[0].gender, select,
                male {You ({{sender.name}}) gave him ({{recipients[0].name}}) a gift.}
                female {You ({{sender.name}}) gave her ({{recipients[0].name}}) a gift.}
                other {You ({{sender.name}}) gave them ({{recipients[0].name}}) a gift.}
            }}
         }
      one { {{ recipients[0].gender, select,
                male {You ({{sender.name}}) gave him ({{recipients[0].name}}) and one other person a gift.}
                female {You ({{sender.name}}) gave her ({{recipients[0].name}}) and one other person a gift.}
                other {You ({{sender.name}}) gave them ({{recipients[0].name}}) and one other person a gift.}
            }}
         }
         other {You ({{sender.name}}) gave {{recipients.length}} people gifts. }
  }}

  <br><br><b>In an attribute</b><br>
  <div attrib="{{recipients.length, plural, offset:1
                  =0 {You ({{sender.name}}) gave no gifts}
                  =1 { {{ recipients[0].gender, select,
                            male {You ({{sender.name}}) gave him ({{recipients[0].name}}) a gift.}
                            female {You ({{sender.name}}) gave her ({{recipients[0].name}}) a gift.}
                            other {You ({{sender.name}}) gave them ({{recipients[0].name}}) a gift.}
                        }}
                     }
                  one { {{ recipients[0].gender, select,
                            male {You ({{sender.name}}) gave him ({{recipients[0].name}}) and one other person a gift.}
                            female {You ({{sender.name}}) gave her ({{recipients[0].name}}) and one other person a gift.}
                            other {You ({{sender.name}}) gave them ({{recipients[0].name}}) and one other person a gift.}
                        }}
                     }
                     other {You ({{sender.name}}) gave {{recipients.length}} people gifts. }
               }}">
      This div has an attribute interpolated with messageformat.  Use the DOM inspector to check it out.
  </div>
</div>
</body>
</html>

<!--
Copyright 2019 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->

******************************************INDEX.HTML*****************************************************
********************************************app.js***************************************************
(function(angular) {
  'use strict';
function Person(name, gender) {
  this.name = name;
  this.gender = gender;
}

angular.module('messageFormatExample', ['ngMessageFormat'])
  .controller('ckCtrl', function($scope, $injector, $parse) {
    var people = [new Person('Alice', 'female'),
                  new Person('Bob', 'male'),
                  new Person('Charlie', 'male')];

    $scope.sender = new Person('Harry Potter', 'male');
    $scope.recipients = people.slice();

    $scope.setNumRecipients = function(n) {
      n = n > people.length ? people.length : n;
      $scope.recipients = people.slice(0, n);
    };

    $scope.setGender = function(person, gender) {
      person.gender = gender;
    };
  });
})(window.angular);




********************************************app.js***************************************************

Comments

Popular posts from this blog

Tutorial 12.12 Creating a Directive that Wraps Other Elements 3

Tutorial 12.14 Creating Directives that Communicate