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

Angular 2 コンポーネント指向の第一歩

Angular 2 コンポーネント指向の第一歩

HTML 5とか勉強会 第64回の登壇にて使用した資料です。

OKUNOKENTARO

April 22, 2016
Tweet

More Decks by OKUNOKENTARO

Other Decks in Programming

Transcript

  1. <!DOCTYPE html> <html> <head> <script src="angular.js"></script> <script src="script.js"></script> </head> <body

    ng-app> <div ng-controller="simpleController"> <strong>First name:</strong> {{firstName}}<br /> <strong>Last name:</strong> <span ng-bind="lastName"></span><br /> <strong>Full name:</strong> {{getFullName()}}<br /> <br /> <label> Set the first name: <input type="text" ng-model="firstName" /> </label><br /> <label> Set the last name: <input type="text" ng-model="lastName" /> </label> </div> </body> </html> AngularJS Hub by Andrea Bresolin is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
  2. <script src="angular.js"></script> <script src="script.js"></script> </head> <body ng-app> <p> <h2>Nested controllers

    with model variables defined directly on the scopes</h2> (typing on an input field, with a data binding to the model, overrides the same variable of a parent scope) </p> <div ng-controller="firstControllerScope"> <h3>First controller</h3> <strong>First name:</strong> {{firstName}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstName"/></label><br /> <br /> <div ng-controller="secondControllerScope"> <h3>Second controller (inside First)</h3> <strong>First name (from First):</strong> {{firstName}}<br /> <strong>Last name (new variable):</strong> {{lastName}}<br /> <strong>Full name:</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstName"/></label><br /> <label>Set the last name: <input type="text" ng-model="lastName"/></label><br /> <br /> <div ng-controller="thirdControllerScope"> <h3>Third controller (inside Second and First)</h3> <strong>First name (from First):</strong> {{firstName}}<br /> <strong>Middle name (new variable):</strong> {{middleName}}<br /> <strong>Last name (from Second):</strong> {{$parent.lastName}}<br /> <strong>Last name (redefined in Third):</strong> {{lastName}}<br /> <strong>Full name (redefined in Third):</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstName"/></label><br /> <label>Set the middle name: <input type="text" ng-model="middleName"/></label><br /> <label>Set the last name: <input type="text" ng-model="lastName"/></label> </div> </div> </div> <br /> <p> <h2>Nested controllers with model variables defined inside objects</h2> (typing on an input field, with a data binding to the model, acts on a specific object without overriding variables) </p> <div ng-controller="firstControllerObj"> <h3>First controller</h3> <strong>First name:</strong> {{firstModelObj.firstName}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstModelObj.firstName"/></label><br /> <br /> <div ng-controller="secondControllerObj"> <h3>Second controller (inside First)</h3> <strong>First name (from First):</strong> {{firstModelObj.firstName}}<br /> <strong>Last name (from Second):</strong> {{secondModelObj.lastName}}<br /> <strong>Full name:</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstModelObj.firstName"/></label><br /> <label>Set the last name: <input type="text" ng-model="secondModelObj.lastName"/></label><br /> <br /> <div ng-controller="thirdControllerObj"> <h3>Third controller (inside Second and First)</h3> <strong>First name (from First):</strong> {{firstModelObj.firstName}}<br /> <strong>Middle name (from Third):</strong> {{thirdModelObj.middleName}}<br /> <strong>Last name (from Second):</strong> {{secondModelObj.lastName}}<br /> <strong>Last name (from Third):</strong> {{thirdModelObj.lastName}}<br /> <strong>Full name (redefined in Third):</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstModelObj.firstName"/></label><br /> <label>Set the middle name: <input type="text" ng-model="thirdModelObj.middleName"/></label><br /> <label>Set the last name: <input type="text" ng-model="thirdModelObj.lastName"/></label> </div> AngularJS Hub by Andrea Bresolin is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
  3. <script src="angular.js"></script> <script src="script.js"></script> </head> <body ng-app> <p> <h2>Nested controllers

    with model variables defined directly on the scopes</h2> (typing on an input field, with a data binding to the model, overrides the same variable of a parent scope) </p> <div ng-controller="firstControllerScope"> <h3>First controller</h3> <strong>First name:</strong> {{firstName}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstName"/></label><br /> <br /> <div ng-controller="secondControllerScope"> <h3>Second controller (inside First)</h3> <strong>First name (from First):</strong> {{firstName}}<br /> <strong>Last name (new variable):</strong> {{lastName}}<br /> <strong>Full name:</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstName"/></label><br /> <label>Set the last name: <input type="text" ng-model="lastName"/></label><br /> <br /> <div ng-controller="thirdControllerScope"> <h3>Third controller (inside Second and First)</h3> <strong>First name (from First):</strong> {{firstName}}<br /> <strong>Middle name (new variable):</strong> {{middleName}}<br /> <strong>Last name (from Second):</strong> {{$parent.lastName}}<br /> <strong>Last name (redefined in Third):</strong> {{lastName}}<br /> <strong>Full name (redefined in Third):</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstName"/></label><br /> <label>Set the middle name: <input type="text" ng-model="middleName"/></label><br /> <label>Set the last name: <input type="text" ng-model="lastName"/></label> </div> </div> </div> <br /> <p> <h2>Nested controllers with model variables defined inside objects</h2> (typing on an input field, with a data binding to the model, acts on a specific object without overriding variables) </p> <div ng-controller="firstControllerObj"> <h3>First controller</h3> <strong>First name:</strong> {{firstModelObj.firstName}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstModelObj.firstName"/></label><br /> <br /> <div ng-controller="secondControllerObj"> <h3>Second controller (inside First)</h3> <strong>First name (from First):</strong> {{firstModelObj.firstName}}<br /> <strong>Last name (from Second):</strong> {{secondModelObj.lastName}}<br /> <strong>Full name:</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstModelObj.firstName"/></label><br /> <label>Set the last name: <input type="text" ng-model="secondModelObj.lastName"/></label><br /> <br /> <div ng-controller="thirdControllerObj"> <h3>Third controller (inside Second and First)</h3> <strong>First name (from First):</strong> {{firstModelObj.firstName}}<br /> <strong>Middle name (from Third):</strong> {{thirdModelObj.middleName}}<br /> <strong>Last name (from Second):</strong> {{secondModelObj.lastName}}<br /> <strong>Last name (from Third):</strong> {{thirdModelObj.lastName}}<br /> <strong>Full name (redefined in Third):</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstModelObj.firstName"/></label><br /> <label>Set the middle name: <input type="text" ng-model="thirdModelObj.middleName"/></label><br /> <label>Set the last name: <input type="text" ng-model="thirdModelObj.lastName"/></label> </div> AngularJS Hub by Andrea Bresolin is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. だいたい こうなるよね
  4. <script src="angular.js"></script> <script src="script.js"></script> </head> <body ng-app> <p> <h2>Nested controllers

    with model variables defined directly on the scopes</h2> (typing on an input field, with a data binding to the model, overrides the same variable of a parent scope) </p> <div ng-controller="firstControllerScope"> <h3>First controller</h3> <strong>First name:</strong> {{firstName}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstName"/></label><br /> <br /> <div ng-controller="secondControllerScope"> <h3>Second controller (inside First)</h3> <strong>First name (from First):</strong> {{firstName}}<br /> <strong>Last name (new variable):</strong> {{lastName}}<br /> <strong>Full name:</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstName"/></label><br /> <label>Set the last name: <input type="text" ng-model="lastName"/></label><br /> <br /> <div ng-controller="thirdControllerScope"> <h3>Third controller (inside Second and First)</h3> <strong>First name (from First):</strong> {{firstName}}<br /> <strong>Middle name (new variable):</strong> {{middleName}}<br /> <strong>Last name (from Second):</strong> {{$parent.lastName}}<br /> <strong>Last name (redefined in Third):</strong> {{lastName}}<br /> <strong>Full name (redefined in Third):</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstName"/></label><br /> <label>Set the middle name: <input type="text" ng-model="middleName"/></label><br /> <label>Set the last name: <input type="text" ng-model="lastName"/></label> </div> </div> </div> <br /> <p> <h2>Nested controllers with model variables defined inside objects</h2> (typing on an input field, with a data binding to the model, acts on a specific object without overriding variables) </p> <div ng-controller="firstControllerObj"> <h3>First controller</h3> <strong>First name:</strong> {{firstModelObj.firstName}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstModelObj.firstName"/></label><br /> <br /> <div ng-controller="secondControllerObj"> <h3>Second controller (inside First)</h3> <strong>First name (from First):</strong> {{firstModelObj.firstName}}<br /> <strong>Last name (from Second):</strong> {{secondModelObj.lastName}}<br /> <strong>Full name:</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstModelObj.firstName"/></label><br /> <label>Set the last name: <input type="text" ng-model="secondModelObj.lastName"/></label><br /> <br /> <div ng-controller="thirdControllerObj"> <h3>Third controller (inside Second and First)</h3> <strong>First name (from First):</strong> {{firstModelObj.firstName}}<br /> <strong>Middle name (from Third):</strong> {{thirdModelObj.middleName}}<br /> <strong>Last name (from Second):</strong> {{secondModelObj.lastName}}<br /> <strong>Last name (from Third):</strong> {{thirdModelObj.lastName}}<br /> <strong>Full name (redefined in Third):</strong> {{getFullName()}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="firstModelObj.firstName"/></label><br /> <label>Set the middle name: <input type="text" ng-model="thirdModelObj.middleName"/></label><br /> <label>Set the last name: <input type="text" ng-model="thirdModelObj.lastName"/></label> </div> AngularJS Hub by Andrea Bresolin is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. このまま 大規模アプリ作りますか
  5. import {Component} from 'angular2/core'; export class Hero { id: number;

    name: string; } @Component({ selector: 'my-app', template: ` <h1>{{title}}</h1> <h2>{{hero.name}} details!</h2> <div><label>id: </label>{{hero.id}}</div> <div> <label>name: </label> <input [(ngModel)]="hero.name" placeholder="name"> </div> ` }) export class AppComponent { title = 'Tour of Heroes'; hero: Hero = { id: 1, name: 'Windstorm' }; }
  6. import {Component} from 'angular2/core'; export class Hero { id: number;

    name: string; } @Component({ selector: 'my-app', template: ` <h1>{{title}}</h1> <h2>{{hero.name}} details!</h2> <div><label>id: </label>{{hero.id}}</div> <div> <label>name: </label> <input [(ngModel)]="hero.name" placeholder="name"> </div> ` }) export class AppComponent { title = 'Tour of Heroes'; hero: Hero = { id: 1, name: 'Windstorm' }; } Componentを定義するための
 デコレーター HTMLテンプレート <my-app></my-app> と書けばその中に展開される このComponentの挙動を
 実装するクラス
  7. import {Component} from 'angular2/core'; export class Hero { id: number;

    name: string; } @Component({ selector: 'my-app', template: ` <h1>{{title}}</h1> <h2>{{hero.name}} details!</h2> <div><label>id: </label>{{hero.id}}</div> <div> <label>name: </label> <input [(ngModel)]="hero.name" placeholder="name"> </div> ` }) export class AppComponent { title = 'Tour of Heroes'; hero: Hero = { id: 1, name: 'Windstorm' }; } このheroが テンプレートのheroに
 バインドされる このtitleが テンプレートのtitleに
 バインドされる
  8. import {Component, Input} from 'angular2/core'; import {Hero} from './hero'; @Component({

    selector: 'my-hero-detail', template: ` <div *ngIf="hero"> <h2>{{hero.name}} details!</h2> <div><label>id: </label>{{hero.id}}</div> <div> <label>name: </label> <input [(ngModel)]="hero.name" placeholder="name"/> </div> </div> ` }) export class HeroDetailComponent { @Input() hero: Hero; }
  9. import {Component, Input} from 'angular2/core'; import {Hero} from './hero'; @Component({

    selector: 'my-hero-detail', template: ` <div *ngIf="hero"> <h2>{{hero.name}} details!</h2> <div><label>id: </label>{{hero.id}}</div> <div> <label>name: </label> <input [(ngModel)]="hero.name" placeholder="name"/> </div> </div> ` }) export class HeroDetailComponent { @Input() hero: Hero; } heroという名のInputを定義
  10. @Component({ selector: 'hero-app', template: ` <h1>Tour of Heroes</h1> <hero-app-main [hero]=hero></hero-app-main>`,

    styles: ['h1 { font-weight: normal; }'], directives: [HeroAppMainComponent] })
  11. @Component({ selector: 'hero-app', template: ` <h1>Tour of Heroes</h1> <hero-app-main [hero]=hero></hero-app-main>`,

    styles: ['h1 { font-weight: normal; }'], directives: [HeroAppMainComponent] }) stylesプロパティに CSSの文字列を配列で記述 このコンポーネント以下にのみ適用できるスタイル