Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Criando Componentes AngularJS

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Criando Componentes AngularJS

Avatar for Ciro Nunes

Ciro Nunes

August 06, 2014
Tweet

More Decks by Ciro Nunes

Other Decks in Programming

Transcript

  1. <div id="tabs"> <ul> <li><a href="#fragment1">One</a></li> <li><a href="#fragment2">Two</a></li> <li><a href="#fragment3">Three</a></li> </ul>

    <div id="fragment1"> <p>Tab 1</p> </div> <div id="fragment2"> <p>Tab 2</p> </div> <div id="fragment3"> <p>Tab 3</p> </div> </div> ! <script> $("#tabs").tabs(); </script> tabs.html
  2. USANDO COMPONENTES DE TERCEIROS $ bower search angular-ui $ bower

    install angular-ui —-save <script src=“bower_components/…/angular-ui.js”></script> index.html
  3. USANDO COMPONENTES DE TERCEIROS $ bower search angular-ui $ bower

    install angular-ui —-save <script src=“bower_components/…/angular-ui.js”></script> index.html angular.module('myApp', ['ui']); app.js
  4. HELLO WORLD angular.module('myDirectives', []); CRIE UM MÓDULO PARA SUAS DIRETIVAS

    angular.module('myApp', ['myDirectives']); INCLUA ESSE MÓDULO NA SUA APLICAÇÃO
  5. HELLO WORLD angular.module('myDirectives') .directive('myGreet' function() { return { restrict: 'E',

    // more later template: '<h1>Hello World!</h1>' }; }); <my-greet></my-greet>
  6. HELLO WORLD angular.module('myDirectives') .directive('myGreet' function() { return { restrict: 'E',

    // more later template: '<h1>Hello World!</h1>' }; }); <my-greet></my-greet> HELLO WORLD
  7. BEST PRATICE: CONVENÇÃO DE NOMENCLATURA USE UM PREFIXO ÚNICO SEGUIDO

    POR "-". (e.g.:>my:component) NÃO USE O PREFIXO "ng:"
  8. BEST PRATICE: CONVENÇÃO DE NOMENCLATURA USE UM PREFIXO ÚNICO SEGUIDO

    POR "-". (e.g.:>my:component) NÃO USE O PREFIXO "ng:" O MAIS COMUM É USAR PREFIXOS DE DUAS LETRAS (e.g.:>"ng:",>"ui:")
  9. // removed for brevity .controller('MyCtrl' function($scope) { $scope.person = 'Ciro';

    )) .directive('myGreet' function() { return { restrict: 'EA', template: '<h1>Hello {{ person }}!</h1>’ }; });
  10. // removed for brevity .controller('MyCtrl' function($scope) { $scope.person = 'Ciro';

    )) .directive('myGreet' function() { return { restrict: 'EA', template: '<h1>Hello {{ person }}!</h1>’ }; });
  11. <div ng-controller="MyCtrl"> <my-greet></my-greet> </div> // removed for brevity .controller('MyCtrl' function($scope)

    { $scope.person = 'Ciro'; )) .directive('myGreet' function() { return { restrict: 'EA', template: '<h1>Hello {{ person }}!</h1>’ }; });
  12. <div ng-controller="MyCtrl"> <my-greet></my-greet> </div> // removed for brevity .controller('MyCtrl' function($scope)

    { $scope.person = 'Ciro'; )) .directive('myGreet' function() { return { restrict: 'EA', template: '<h1>Hello {{ person }}!</h1>’ }; }); HIDDEN KNOWLEDGE!
  13. .controller('MyCtrl' function($scope) { $scope.person = 'Ciro'; )) .directive('myGreet' function() {

    return { restrict: 'EA', template: '<h1>Hello {{ person }}!</h1>’, scope: { person: '@' } }; });
  14. .controller('MyCtrl' function($scope) { $scope.person = 'Ciro'; )) .directive('myGreet' function() {

    return { restrict: 'EA', template: '<h1>Hello {{ person }}!</h1>’, scope: { person: '@' } }; });
  15. <div ng-controller="MyCtrl"> <my-greet person="{{ person }}"></my-greet> </div> .controller('MyCtrl' function($scope) {

    $scope.person = 'Ciro'; )) .directive('myGreet' function() { return { restrict: 'EA', template: '<h1>Hello {{ person }}!</h1>’, scope: { person: '@' } }; });
  16. <div ng-controller="MyCtrl"> <my-greet person="{{ person }}"></my-greet> </div> .controller('MyCtrl' function($scope) {

    $scope.person = 'Ciro'; )) .directive('myGreet' function() { return { restrict: 'EA', template: '<h1>Hello {{ person }}!</h1>’, scope: { person: '@' } }; });
  17. .directive('myGreet', function() { return { link: function($scope, $element, $attrs) {

    $element.on('click', function() { // do something }); } }; });