Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| slam4:richclient:angularjs:elements [2015/01/01 18:59] – [Directive] jcheron | slam4:richclient:angularjs:elements [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 73: | Ligne 73: | ||
| }); | }); | ||
| </ | </ | ||
| + | |||
| + | Directive complète : | ||
| + | |||
| + | <sxh javascript> | ||
| + | myApp.directive( ' | ||
| + | return { | ||
| + | restrict: ' | ||
| + | controller: function( $scope, $element, $attrs, $transclude ) { | ||
| + | // Controller code goes here. | ||
| + | }, | ||
| + | compile: function compile( tElement, tAttributes, | ||
| + | // Compile code goes here. | ||
| + | return { | ||
| + | pre: function preLink( scope, element, attributes, controller, transcludeFn ) { | ||
| + | // Pre-link code goes here | ||
| + | }, | ||
| + | post: function postLink( scope, element, attributes, controller, transcludeFn ) { | ||
| + | // Post-link code goes here | ||
| + | } | ||
| + | }; | ||
| + | } | ||
| + | }; | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | ===== Injection de dépendance (dependency injection) | ||
| + | Elle permet d' | ||
| + | |||
| + | Injection avec tableau (conseillé) | ||
| + | <sxh javascript; | ||
| + | angular.module(" | ||
| + | // ... | ||
| + | }]); | ||
| + | </ | ||
| + | |||
| + | Injection avec la propriété **$inject** | ||
| + | |||
| + | <sxh javascript; | ||
| + | var MyController = function($scope, | ||
| + | // ... | ||
| + | } | ||
| + | MyController.$inject = [" | ||
| + | angular.module(" | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | |||
| + | |||
| + | **<fc # | ||
| + | |||
| + | <sxh javascript; | ||
| + | angular.module(" | ||
| + | // ... | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Vue (HTML File) ===== | ||
| + | |||
| + | <sxh html; | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <meta charset=" | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | <body data-ng-app> | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Création d' | ||
| + | ==== Factory ==== | ||
| + | |||
| + | <sxh javascript> | ||
| + | monApp.factory(' | ||
| + | return { | ||
| + | " | ||
| + | " | ||
| + | // faire qqchose | ||
| + | } | ||
| + | } | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | **maFactory** est un singleton (une seule instance), et devient injectable : | ||
| + | |||
| + | Injection dans un contrôleur : | ||
| + | |||
| + | <sxh javascript> | ||
| + | app.controller(" | ||
| + | // | ||
| + | $scope.member=maFactory.member; | ||
| + | maFactory.method(); | ||
| + | ... | ||
| + | }]); | ||
| + | </ | ||
| + | |||
| + | ==== Service ==== | ||
| + | |||
| + | Le service permet également de créer un objet injectable, mais d'une autre façon : | ||
| + | |||
| + | <sxh javascript> | ||
| + | monApp.service(' | ||
| + | this.name = " | ||
| + | this.id = null; | ||
| + | this.login = function(){ | ||
| + | // faire un truc pour se connecter | ||
| + | } | ||
| + | this.logout = function(){ | ||
| + | // faire un truc pour se déconnecter | ||
| + | } | ||
| + | }); | ||
| + | </ | ||
| + | |||