1. 程式人生 > >Angular2 @Input 和 @Input 用法

Angular2 @Input 和 @Input 用法

@Input

@Input是用來定義模組的輸入的,用來讓父模組往子模組傳遞內容:

@Component({
  selector: 'bank-account',
  template: `
    Bank Name: {{bankName}}
    Account Id: {{id}}
  `
})
class BankAccount {
  @Input() bankName: string;
  @Input('account-id') id: string;
  // this property is not bound, and won't be automatically updated by Angular
normalizedBankName: string; } @Component({ selector: 'app', template: ` < bank-account bank-name="RBC" account-id="4747">< bank-account> `, directives: [BankAccount] }) class App {}

在這個例子中,父模組是app,子模組是BankAccount, 在app中我們想往子模組裡面傳遞back-name和account-id這兩個資訊,可以通過property的方式最簡單直接,而子模組要接受這個 property就要用@Input來接收。子模組會在BankAccount裡面直接接收傳遞過來的property。傳遞的方式可以是駝峰法,也可以直接寫在input裡面,就如
例子裡面寫的那樣。要記住一點的是父模組在引用子模組的時候是用的[]。

@Output

子模組想自定義一些event傳遞給父模組又該怎麼做呢?這時候就要用到@Output了。

@Directive({
  selector: 'interval-dir',
})
class IntervalDir {
  @Output() everySecond = new EventEmitter();
  @Output('everyFiveSeconds') five5Secs = new EventEmitter();
  constructor() {
    setInterval(() => this.everySecond.emit("event"
), 1000)
; setInterval(() => this.five5Secs.emit("event"), 5000); } } @Component({ selector: 'app', template: ` < interval-dir (every-second)="everySecond()" (every-five-seconds)="everyFiveSeconds()"> < /interval-dir> `, directives: [IntervalDir] }) class App { everySecond() { console.log('second'); } everyFiveSeconds() { console.log('five seconds'); } }

因為在普通的html裡面,比如button我們會有onclick這種event來監聽這個button是否被按了,然後angular2也完全允許我們自定義這種event listening的模式,我們可以給任何模組定義這種event,當觸發了event之後就會從子模組往父模組傳遞子模組的資訊。

再這個例子裡,父模組是app,子模組是IntervalDir, 然後在子模組定義event觸發的條件,比如每隔1s要觸發事件,每隔5s要觸發事件。然後當父模組監聽到這些事件時可以做相應的操作。當然我們如果想傳遞資訊,可以在new EventEmitter()裡面加入我們想要傳遞的東西,然後在使用的時候加入函式的引數裡面。比如我們想傳遞一個
test,我們只需要這麼改:

class IntervalDir {
@Output() everySecond = new EventEmitter(test);
}
< interval-dir (every-second)=”everySecond(test)” (every-five-seconds)=”everyFiveSeconds()”>
everySecond(test) { console.log(test); }

而在這裡,父模組在引用子模組的時候是用的()。

[(ngModel)]

最後當然是最經典的angular特有的ngModel雙向binding了。而這個特性是< input >特有的。我們注意到了 [(ngModel)] 既有[]也有()。沒錯它既有Input的特性也有Output的特性。

< input [(ngModel)]="hero.name" placeholder="name" > 

directives

主要是加入子模組的模組,當我們定義了一個模組之後,如果我們想要它被別的模組使用,必須定義一個出口被別的模組使用,比如app.component這個模組最後就要:

export class AppComponent {} 

讓angular2知道它是一個AppComponent模組,然後在boot裡面可以加入這個模組。同理我也export了HeroComponent,HeroFormComponent以及HeroesListComponent模組。然後在app.component的一開始就import進來,然後就要在directive加入這些模組然後在template才能識別相應的selector。如果
不在directive中引入相應的模組,那在compile的時候template就不知道相應的selector是啥就不能正常的render了。

providers

這個主要用來加入service的模組。當我們想在模組之中使用service的時候,和directive相同的道理,我們需要加入provider這樣這個模組就知道使用的service的provider。小G在用angular的過程中碰到最多的問題就是missing provider的問題,在angular1中可能是由於service的dependency之間有loop,
表示service互相依賴,會有問題。但angular2由於模組化了這個dependency injection的問題得到了很好的解決。如果再碰到這類問題首先要檢查模組是否加入了service provider。

styles

這個主要定義這個模組使用的css,不用放到外部的style.css裡面。這樣這個模組的css完全由自己掌控,是不是方便了很多?



< li *ngFor="#hero of heroes"
    [class.selected]="hero === selectedHero"
    (click)="onSelect(hero)">
    < span class="badge">{{hero.id}}< /span> {{hero.name}}
< /li >

在html裡面用ngFor來迴圈這個list然後把list裡面的英雄輸出到html裡面。在這裡的語法和angular1完全不一樣了。在angular1裡面,完全沒有以及#的符號,但是在angular2裡面,和#確實很關鍵的語法。
( )這個在ngFor的字首表示整個< li >元素以及它的子元素合成一個master模板。如果不想使用( ),ngFor需要被包含在一個template的元素裡面:



< template ngFor #hero [ngForOf]="heroes" >
  < hero-detail [hero]="hero" >< /hero-detail >
< /template >

Angular2還有這個語法的就是ngIf。如果不用()也需要把ngIf包含在template裡面。

相關推薦

Angular2InputOutput用法及示例

                     對於angular2中的Input和Output可以和angularjs中指令作類比。Input相當於指令的值繫結,無論是單向的(@)還是雙向的(=)。都是將父作用域的值“輸入”到子作用域中,然後子作用域進行相關處理。Output相當於指令的方法繫結,子作用域觸發事件

Angular2 @Input @Input 用法

@Input @Input是用來定義模組的輸入的,用來讓父模組往子模組傳遞內容: @Component({ selector: 'bank-account', template: ` Bank Name: {{bankName}} A

jQuery中input:input選擇器的區別

(1) input,標籤選擇器,僅僅選擇input元素<input type="text">; (2) :input,偽類選擇器,選擇表單中的input ,select, textarea, button元素. 示例如下: html: <

Python 內置函數raw_input()input()用法區別

標準 換行 字符串 輸入 我們 print raw_input http bsp 我們知道python接受輸入的raw_input()和input() ,在python3 輸入raw_input() 去掉樂,只要用input() 輸入,input 可以接收一個Pyt

Python:raw_input input用法

使用input和raw_input都可以讀取控制檯的輸入,但是input和raw_input在處理數字時是有區別的 純數字輸入 當輸入為純數字時     input返回的是數值型別,如int,float     raw_inpo

Angular2父子元件之間資料傳遞:@Input@Output (上)

為了讓大家學習起來輕鬆、易懂,小編儘量做到篇幅短,語言通俗易懂,知識點分段來講,以免太長了看起來很累,也很容易失去耐心閱讀下去,希望大家理解和支援,同時希望大家點贊和分享出去,讓更多的志同道合的朋友來學習 Angular 提供了@Input和@Output語法來處理元

python中eval()函式input()函式用法解析

1.eval()函式 eval(<字串>)能夠以Python表示式的方式解析並執行字串,並將返回結果輸出。eval()函式將去掉字串的兩個引號,將其解釋為一個變數。 作用: a. 處理數字 單引號,雙引號,eval()函式都將其解釋為int型別;三引號

Python input()raw_input()的區別

color 規則 自己 () 必須 ring 格式 class 但是   區別嘛,就是raw_input()隨便輸都是字符串,而input()必須按照Python的規則來~ raw_input() name=raw_input(‘輸入姓名:‘) age=raw_inp

inputtextarea區別

code type textarea put cnblogs tar 滾動 text blog 1. input是單行文本,textarea是多行文本,可以帶滾動條2. input的value放在標簽裏面 <input type="text" value="beij

input button的區別

lena 一是 ima 小寫 size 服務 tor 自動刷新 element 一,區別一 先來看一個問題 1 <input type="button" class="btn-upload bg-org-code" name="yushow" id="yush

HTML中buttoninput button的區別

sta ram window 除了 水平 其中 插入 而是 ext button和input button的區別 一句話概括主題:<button>具有<input type="button" ... >相同的作用但是在可操控性方面更加強大。 HTML

第二天 第二彈 iframe內嵌框架 from表單input的運用

tex class AC -s AI bsp box 內嵌 align 先是iframe框架 接下來是代碼的運用 <html> <head> <title>IFRAME</title> <meta charset

js 監測from表單中的inputselect,時時監測,沒有輸入或選擇信息報錯,不允許提交數據

height ssss txt input OS 表達 tip eight html 1.html 代碼為 在input和select同級元素中添加 .error的標簽,用來存放報錯信息 <form action="" method="post" enctype="

SpringMVC from 表單標簽 input 表單標簽

cape rep mission photo div eml wid arch ext 剛學習很懵 不知道還有springmvc 自己的表單 於是乎就上網查了一下 這個真的好用多啦 剛學習很懵 不知道還有springmvc 自己的表單 於是乎就上網查了一下 這

inputoutput常用模塊的講解使用(logstash)

dto 路徑 批量查詢 nts sts pre art com send 1 ELK 是一個實時分布式的日誌分析平臺ELK 是一整套的解決方案(E)lasticsearch -- 數據庫(L)ogstash -- 收集日誌、標準化的程序(K)ibana

vue elementui table 雙擊單元格實現編輯,聚焦,失去焦點,顯示隱藏inputspan

ntc 單元 src func cell Dimension === http nts <el-table :data="tableData" class="tb-edit" style="width: 100%"

argo的輸入輸出--outputinput輸出目錄或檔案到下一步驟

轉載請註明出處: argo的輸入輸出–output和input輸出目錄或檔案到下一步驟 有部分場景需要使用output把目錄或者檔案傳遞到下一個步驟。 argo提供了兩種方式 一種是引數方式parameter 一種是元件方式artifacts 各自適用於不同的場景,引數方式是

Angular4父子元件傳遞@Input@Output

Angular提供了@Input和@Output語法來處理元件資料輸入和輸出。 @Input Input輸入屬性,父元件向子元件傳遞資訊。 父元件HTML程式碼: <div> 這是父元件中輸入值:<br><br>

BootStrap讓兩個控制元件在一行顯示(labelinput同行)

1 、新增class=“form-inline” <div class="row"> <div> <label class="form-inline" />參加單位:

表單驗證——inputrequired衝突解決

1. input和required衝突解決 Question: 使用表單提交驗證必填欄位時,若同時給 input[type="file"] 新增 readonly 以及 required 屬性,則發現,required必填驗證失效,既不會提示必填,也可以成功提交。 解決方法:將 r