angular4自定義組件詳解
在 Angular 中,我們可以使用 {{}} 插值語(yǔ)法實(shí)現(xiàn)數(shù)據(jù)綁定。
新建組件
$ ng generate component simple-form --inline-template --inline-style # Or $ ng g c simple-form -it -is # 表示新建組件,該組件使用內(nèi)聯(lián)模板和內(nèi)聯(lián)樣式 //會(huì)自動(dòng)為simple-form生成simple-form.component.ts文件,文件中的selector為:app-simple-form,自動(dòng)添加了app-前綴
輸出:
installing component create src/app/simple-form/simple-form.component.spec.ts // 用于單元測(cè)試 create src/app/simple-form/simple-form.component.ts // 新建的組件 update src/app/app.module.ts //Angular CLI 會(huì)自動(dòng)更新 app.module.ts 文件。把新建的組件添加到 NgModule 的 declarations
數(shù)組中
app.module.ts更新后:
@NgModule({
declarations: [
AppComponent,
SimpleFormComponent
],
...
})
export class AppModule { }
創(chuàng)建 UserComponent 組件
import { Component } from '@angular/core';
@Component({ //Component 裝飾器來(lái)定義組件的元信息
selector: 'sl-user',
template: `
<h2>大家好,我是{{name}}</h2>
<p>我來(lái)自<strong>{{address.province}}</strong>省,
<strong>{{address.city}}</strong>市
</p>
<p>{{address | json}}</p>//Angular 內(nèi)置的 json 管道,來(lái)顯示對(duì)象信息
`, })
//定義組件類
export class UserComponent {
name = 'name';
address = { province: 'province', city: 'city' }
}
//使用構(gòu)造函數(shù)初始化數(shù)據(jù)
export class UserComponent {
name: string;
address: any;
constructor() {
this.name = 'name';
this.address = {
province: 'province',
city: 'city'
}
}
}
//接口使用
interface Address {
province: string;
city: string;
}
export class UserComponent {
name: string;
address: Address;
constructor(){
this.name = 'name';
this.address = {
province: 'province',
city: 'city'
}
}
}
定義數(shù)據(jù)接口( TypeScript 中的接口是一個(gè)非常靈活的概念,除了可用于對(duì)類的一部分行為進(jìn)行抽象以外,也常用于對(duì)「對(duì)象的形狀(Shape)」進(jìn)行描述。)
interface Person {
name: string;
age: number;
}
let semlinker: Person = {
name: 'semlinker',
age: 31
};
聲明 UserComponent 組件
// ...
import { UserComponent } from './user.component';//載入
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent, UserComponent],//聲明
bootstrap: [ AppComponent ]
})
export class AppModule { }
在AppComponent中使用 UserComponent 組件
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<sl-user></sl-user> //UserComponent 的 selector
`,
})
export class AppComponent {}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Angular 4.x 動(dòng)態(tài)創(chuàng)建組件
- Angular2學(xué)習(xí)教程之組件中的DOM操作詳解
- 詳解angular2封裝material2對(duì)話框組件
- angular2倒計(jì)時(shí)組件使用詳解
- Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解
- Angular2利用組件與指令實(shí)現(xiàn)圖片輪播組件
- Angularjs 創(chuàng)建可復(fù)用組件實(shí)例代碼
- Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解(下)
- Angular2開(kāi)發(fā)——組件規(guī)劃篇
- Angular2自定義分頁(yè)組件
相關(guān)文章
基于AngularJS實(shí)現(xiàn)的工資計(jì)算器實(shí)例
這篇文章主要介紹了基于AngularJS實(shí)現(xiàn)的工資計(jì)算器,結(jié)合具體實(shí)例形式分析了AngularJS數(shù)值計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
AngularJS監(jiān)聽(tīng)ng-repeat渲染完成的方法
這篇文章主要介紹了AngularJS監(jiān)聽(tīng)ng-repeat渲染完成的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
詳解Monaco?Editor中的Keybinding機(jī)制
這篇文章主要為大家介紹了詳解Monaco?Editor中的Keybinding機(jī)制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
angular使用bootstrap方法手動(dòng)啟動(dòng)的實(shí)例代碼
本篇文章主要介紹了angular使用bootstrap方法手動(dòng)啟動(dòng)的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
對(duì)angularJs中2種自定義服務(wù)的實(shí)例講解
今天小編就為大家分享一篇對(duì)angularJs中2種自定義服務(wù)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
angularjs實(shí)現(xiàn)文字上下無(wú)縫滾動(dòng)特效代碼
這篇文章主要介紹了angularjs實(shí)現(xiàn)文字上下無(wú)縫滾動(dòng)特效代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Angularjs通過(guò)指令監(jiān)聽(tīng)ng-repeat渲染完成后執(zhí)行腳本的方法
指令是angular的核心功能之一,用好了事半功倍,監(jiān)聽(tīng)ng-repeat執(zhí)行狀態(tài)僅僅是它功能的冰山一角吧。下面這篇文章主要介紹了Angularjs通過(guò)指令監(jiān)聽(tīng)ng-repeat渲染完成后執(zhí)行腳本的方法,需要的朋友可以參考下。2016-12-12
詳解Angular2學(xué)習(xí)筆記之Html屬性綁定
本篇文章主要介紹了詳解Angular2學(xué)習(xí)筆記之Html屬性綁定,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

