這篇文章將為大家詳細講解有關(guān)angular2中@input和@output的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:主機域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、承德縣網(wǎng)站維護、網(wǎng)站推廣。
angular2 @input和@output理解
先做個比方,然后奉上代碼
比如:
<talk-cmp [talk]="someExp" (rate)="eventHandler($event.rating)">
input, [talk]="someExp" 這個標(biāo)簽可以理解為一個專門的監(jiān)聽器,監(jiān)聽父組件傳遞過來的someExp參數(shù),并存入自身組件的talk變;好像是開了個后門,允許且只允許父組件的someExp進入,一旦進入立刻抓進一個叫talk的牢房,然后子組件中就可以通過@Input來定義這個變量talk然后使用它。
output ,(click)="eventHandler($event.rating) 這個意思是, 當(dāng)子組件的click事件被觸發(fā),就執(zhí)行父組件的eventHandler函數(shù),并把子組件的參數(shù)$event.rating傳遞給父組件的eventHandler函數(shù);就好像,當(dāng)小孩子一哭(執(zhí)行click事件),他的母親立刻把他抱在懷里(執(zhí)行母親的eventHandler),同時母親獲得了小孩子的一些參數(shù)($event.rating)
1、@input()
父組件 father.component.ts 提供數(shù)據(jù)
import {Component} from "@angular/core";
@Component({
selector: "my-father",
templateUrl: "father.html"
})
export class FatherComponent {
data: Array<Object>;
constructor() {
this.data = [
{
"id": 1,
"name": "html"
},
{
"id": 2,
"name": "css"
},
{
"id": 3,
"name": "angular"
},
{
"id": 4,
"name": "ionic"
},
{
"id": 5,
"name": "node"
}
]
}
}模板文件 father.html
<h2>父組件</h2> // 包含子組件, 并使用屬性傳遞數(shù)據(jù)過去 <my-child [info]="data"></my-child>
子組件 child.component.ts 獲取數(shù)據(jù)
import {Component, Input} from "@angular/core";
@Component({
selector: "my-child",
templateUrl: "child.html"
})
export class ChildComponent {
// 使用@Input獲取傳遞過來的數(shù)據(jù)
@Input()
info: Array<Object>;
constructor() {
}
}子組件 child.html模板文件
<ul>
<li *ngFor="let item of info">
{{item.name}}
</li>
</ul>2、@Output()
子組件three-link.component.ts
1. 引入
import {Component, OnInit, Output, EventEmitter} from "@angular/core";2. 定義輸出變量
export class ThreeLinkComponent {
province: string;
// 輸出一下參數(shù)
@Output() provinceOut = new EventEmitter();
constructor() {
this.province = "陜西";
}
}3. 事件出發(fā),發(fā)射變量給父組件
provinceChange() {
// 選擇省份的時候發(fā)射省份給父組件
this.provinceOut.emit(this.province);
}父組件模板
<!--三級聯(lián)動組件--> <three-link (provinceOut)="recPro($event)"></three-link>
父組件
// 函數(shù)接受子函數(shù)傳遞過來的變量, 子函數(shù)中emit的時候觸發(fā)這個函數(shù)。
recPro(event) {
this.province = event;
}關(guān)于“angular2中@input和@output的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
網(wǎng)站題目:angular2中@input和@output的示例分析
瀏覽路徑:http://chinadenli.net/article24/jhgece.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、網(wǎng)站制作、定制開發(fā)、品牌網(wǎng)站設(shè)計、App開發(fā)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)