Différences
Ci-dessous, les différences entre deux révisions de la page.
| slam4:richclient:angularjs:bases [2015/10/01 23:50] – modification externe 127.0.0.1 | slam4:richclient:angularjs:bases [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 6: | Ligne 6: | ||
| Installer AngularJS Eclipse Plugin ([[https:// | Installer AngularJS Eclipse Plugin ([[https:// | ||
| - | **Avec PhpStorm :** | + | **Avec PhpStorm |
| voir [[http:// | voir [[http:// | ||
| ===== -- Téléchargement ===== | ===== -- Téléchargement ===== | ||
| Ligne 23: | Ligne 23: | ||
| <sxh html; | <sxh html; | ||
| - | <script src="http://ajax.googleapis.com/ | + | <script src="https://cdnjs.cloudflare.com/ |
| </ | </ | ||
| </ | </ | ||
| - | Quoi qu'en disent certains, il est préférable d' | + | Quoi qu'en disent certains, il est préférable d' |
| ===== -- Premiers pas ===== | ===== -- Premiers pas ===== | ||
| ==== Traditionnel Hello world : ==== | ==== Traditionnel Hello world : ==== | ||
| Ligne 33: | Ligne 33: | ||
| <sxh html; | <sxh html; | ||
| < | < | ||
| - | < | + | < |
| < | < | ||
| - | <meta charset=" | + | |
| - | < | + | < |
| - | <script | + | <script src="https://cdnjs.cloudflare.com/ |
| - | src="http://ajax.googleapis.com/ | + | |
| </ | </ | ||
| < | < | ||
| - | < | + | |
| - | <br> Message : {{message}} | + | <br> Message : {{hello.message}} |
| - | </ | + | </ |
| - | < | + | |
| - | function | + | |
| - | $scope.message = "Hello world"; | + | </ |
| - | } | + | |
| - | </ | + | |
| </ | </ | ||
| </ | </ | ||
| </ | </ | ||
| - | * **ng-app** ou **data-ng-app** définit l' | + | * **ng-app** ou **data-ng-app** définit l' |
| * **ng-controller** ou **data-ng-controller** définit la zone d' | * **ng-controller** ou **data-ng-controller** définit la zone d' | ||
| * Les accolades **< | * Les accolades **< | ||
| Ligne 60: | Ligne 57: | ||
| ==== Hello world modifié ==== | ==== Hello world modifié ==== | ||
| - | On change maintenant la portée du contrôleur (sur balise html de la page), et on ajoute de quoi modifier le destinataire du Hello **aQui** | + | On ajoute de quoi modifier le message |
| - | <sxh html> | + | <sxh html; |
| < | < | ||
| - | < | + | < |
| < | < | ||
| - | <meta charset=" | + | |
| - | < | + | < |
| - | <script | + | <script src="https://cdnjs.cloudflare.com/ |
| - | src="http://ajax.googleapis.com/ | + | |
| </ | </ | ||
| < | < | ||
| - | < | + | |
| - | Message : <input type=" | + | <input type=" |
| - | < | + | < |
| - | </ | + | </ |
| - | < | + | |
| - | function | + | |
| - | $scope.aQui= " | + | </ |
| - | } | + | |
| - | </ | + | |
| </ | </ | ||
| </ | </ | ||
| Ligne 161: | Ligne 155: | ||
| </ | </ | ||
| - | ==== -- Modules ==== | + | ==== -- Modules |
| Un module est un conteneur d' | Un module est un conteneur d' | ||
| Chaque module d'une application correspond logiquement à une fonctionnalité. | Chaque module d'une application correspond logiquement à une fonctionnalité. | ||
| Ligne 185: | Ligne 179: | ||
| Un contrôleur est défini par un constructeur permettant d' | Un contrôleur est défini par un constructeur permettant d' | ||
| - | |||
| - | Exemple en dehors d'un module : | ||
| - | <sxh html> | ||
| - | <div ng-app="" | ||
| - | |||
| - | Nom : <input type=" | ||
| - | Prénom : <input type=" | ||
| - | <br> | ||
| - | Nom et prénom : {{nomComplet()}} | ||
| - | |||
| - | </ | ||
| - | |||
| - | < | ||
| - | function personneController($scope) { | ||
| - | $scope.prenom = " | ||
| - | $scope.nom = " | ||
| - | $scope.fullName = function() { | ||
| - | return $scope.nom + " " + $scope.prenom; | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | </ | ||
| Exemple de contrôleur dans un module : | Exemple de contrôleur dans un module : | ||
| Ligne 235: | Ligne 207: | ||
| voir [[https:// | voir [[https:// | ||
| + | |||
| + | |||
| + | ==== ControllerAs et $scope ==== | ||
| + | L' | ||
| + | |||
| + | <sxh javascript; | ||
| + | var myApp = angular.module(' | ||
| + | |||
| + | myApp.controller(' | ||
| + | this.value=0; | ||
| + | this.carre = function(value) { return value * value; }; | ||
| + | }]); | ||
| + | |||
| + | </ | ||
| + | |||
| + | La vue associée est la suivante : | ||
| + | <sxh html> | ||
| + | < | ||
| + | < | ||
| + | <input type=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| ==== -- $scope ==== | ==== -- $scope ==== | ||
| Ligne 260: | Ligne 255: | ||
| </ | </ | ||
| <script | <script | ||
| - | src="http://ajax.googleapis.com/ | + | src="https://cdnjs.cloudflare.com/ |
| <script src=" | <script src=" | ||
| </ | </ | ||
| Ligne 319: | Ligne 314: | ||
| < | < | ||
| <script | <script | ||
| - | src="http://ajax.googleapis.com/ | + | src="https://cdnjs.cloudflare.com/ |
| <script src=" | <script src=" | ||
| </ | </ | ||
| Ligne 328: | Ligne 323: | ||
| </ | </ | ||
| </ | </ | ||
| + | |||
| + | === Utilisation du service ngCookies === | ||
| + | |||
| + | Le service **ngCookie** n'est pas intégré par défaut à angular, il est donc nécessaire d' | ||
| + | <sxh html; | ||
| + | <script src=" | ||
| + | </ | ||
| + | |||
| + | Il faut ensuite intégrer ngCookies en tant que dépendance dans l' | ||
| + | |||
| + | <sxh javascript; | ||
| + | var App = angular.module(' | ||
| + | </ | ||
| + | |||
| + | nbCookies peut ensuite être injecté dans les contrôleur le nécessitant : | ||
| + | |||
| + | <sxh javascript; | ||
| + | app.controller(" | ||
| + | // Retrieving a cookie | ||
| + | var favoriteCookie = $cookies.get(' | ||
| + | // Setting a cookie | ||
| + | $cookies.put(' | ||
| + | }]); | ||
| + | </ | ||
| + | |||
| === Création d'un service === | === Création d'un service === | ||