基礎(chǔ)用法

創(chuàng)建一組組建以后,會(huì)生成
css→主要負(fù)責(zé)樣式
html→負(fù)責(zé)網(wǎng)頁(yè)顯示
ts→負(fù)責(zé)主要邏輯

import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';

//
@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {

//定義bean屬性
  hero: Hero = {
    id: 1,
    name: 'Windstorm'
  };

  constructor() { }

  ngOnInit() {
  }

}
//定義實(shí)體類
export class Hero {
  id: number;
  name: string;
}

@Component 是個(gè)裝飾器函數(shù),用于為該組件指定 Angular 所需的元數(shù)據(jù)
CLI 自動(dòng)生成了三個(gè)元數(shù)據(jù)屬性:
selector— 組件的選擇器(CSS 元素選擇器)
templateUrl— 組件模板文件的位置。
styleUrls— 組件私有 CSS 樣式表文件的位置。

ngOnInit 是一個(gè)生命周期鉤子,Angular 在創(chuàng)建完組件后很快就會(huì)調(diào)用 ngOnInit。這里是放置初始化邏輯的好地方。
始終要 export 這個(gè)組件類,以便在其它地方(比如 AppModule)導(dǎo)入它。

前端設(shè)定

<h2>{{hero.name | uppercase}} Details</h2>
<div><span>id: </span>{{hero.id}}</div>
<div>
  <label>name:
    <input [(ngModel)]="hero.name" placeholder="name">
  </label>
</div>

把組件綁定在主頁(yè)面上

<h1>{{title}}</h1>
<app-heroes></app-heroes>

雙向綁定

<div>
  <label>name:
    <input [(ngModel)]="hero.name" placeholder="name">
  </label>
</div>

[(ngModel)] 是 Angular 的雙向數(shù)據(jù)綁定語(yǔ)法。
這里把 hero.name 屬性綁定到了 HTML 的 textbox 元素上,以便數(shù)據(jù)流可以雙向流動(dòng):從 hero.name 屬性流動(dòng)到 textbox,并且從 textbox 流回到 hero.name 。
缺少 FormsModule
注意,當(dāng)你加上 [(ngModel)] 之后這個(gè)應(yīng)用無(wú)法工作了。

AppModule
Angular 需要知道如何把應(yīng)用程序的各個(gè)部分組合到一起,以及該應(yīng)用需要哪些其它文件和庫(kù)。 這些信息被稱為元數(shù)據(jù)(metadata)。

有些元數(shù)據(jù)位于 @Component 裝飾器中,你會(huì)把它加到組件類上。 另一些關(guān)鍵性的元數(shù)據(jù)位于 @NgModule 裝飾器中。

最重要的 @NgModule 裝飾器位于頂級(jí)類 AppModule 上。

Angular CLI 在創(chuàng)建項(xiàng)目的時(shí)候就在 src/app/app.module.ts 中生成了一個(gè) AppModule 類。 這里也就是你要添加 FormsModule 的地方。

導(dǎo)入 FormsModule
打開(kāi) AppModule (app.module.ts) 并從 @angular/forms 庫(kù)中導(dǎo)入 FormsModule 符號(hào)。
然后把 FormsModule 添加到 @NgModule 元數(shù)據(jù)的 imports 數(shù)組中,這里是該應(yīng)用所需外部模塊的列表。

import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
imports: [
  BrowserModule,
  FormsModule
],

每個(gè)組件都必須聲明在一個(gè)Ngodule中

在 src/app/app.module.ts 中
import { HeroesComponent } from './heroes/heroes.component';
declarations[  AppComponent,  HeroesComponent  ],

數(shù)組相關(guān)

定義數(shù)組

import { Hero } from './hero';

export const HEROES: Hero[] = [
  { id: 11, name: 'Mr. Nice' },
  { id: 12, name: 'Narco' },
  { id: 13, name: 'Bombasto' },
  { id: 14, name: 'Celeritas' },
  { id: 15, name: 'Magneta' },
  { id: 16, name: 'RubberMan' },
  { id: 17, name: 'Dynama' },
  { id: 18, name: 'Dr IQ' },
  { id: 19, name: 'Magma' },
  { id: 20, name: 'Tornado' }
];

給一個(gè)組件添加該數(shù)組元素

import { HEROES } from '../mock-heroes';

export class HeroesComponent implements OnInit {
  heroes = HEROES;

前端循環(huán)輸出

<h2>My Heroes</h2>
<ul class="heroes">
  <li *ngFor="let hero of heroes"
//循環(huán)輸出,let of 顯示元素,數(shù)組元素
    [class.selected]="hero === selectedHero"
    (click)="onSelect(hero)">
//綁定事件,事件方法也在組件ts中編寫(xiě)
    <span class="badge">{{hero.id}}</span> {{hero.name}}
  </li>
</ul>

<div *ngIf="selectedHero">

  <h2>{{selectedHero.name | uppercase}} Details</h2>
  <div><span>id: </span>{{selectedHero.id}}</div>
  <div>
    <label>name:
      <input [(ngModel)]="selectedHero.name" placeholder="name">
    </label>
  </div>

</div>

主從組件

一個(gè)組件依從另外一個(gè)組件
從件中導(dǎo)入

import { Hero } from '../hero';
import { Component, OnInit, Input } from '@angular/core';
@Input() hero: Hero;

在主件中顯示從件

<app-hero-detail [hero]="selectedHero"></app-hero-detail>

服務(wù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容