2016-08-05 75 views
5

你好,我正在使用Angular2,想要獲取服務器併爲我在ngFor中獲得的每個ID獲取一些值。在ngFor中調用函數在angular2中

<div *ngFor="let item in items"> 
    <span> here call a function that do something with 'item' and return something new <span> 
</div> 
+0

聽起來像一個奇怪的要求。你想達到什麼目的? –

+0

@GünterZöchbauer例如,以某種複雜的方式生成項目屬性的顯示名稱。這並不奇怪。 – user2061057

+0

在代碼中執行並將其分配給每個'item',然後在'* ngFor'中使用它。這種來自模板綁定的函數調用是不鼓勵的。 –

回答

2

更好的做法是在訂閱中對ngOnInit中的每個項目進行這樣的函數調用,然後在轉換後用* ngFor顯示它們。

和變化:

<div *ngFor="let item in items"> 

<div *ngFor="let item of items"> 
+0

你會提供一些你上面建議的方式嗎? – Keshav

5

您可以使用自定義的指令,呼籲每個迭代的方法:

import { Directive, Output, EventEmitter, Input, SimpleChange} from '@angular/core'; 

@Directive({ 
    selector: '[onCreate]' 
}) 
export class OnCreate { 

    @Output() onCreate: EventEmitter<any> = new EventEmitter<any>(); 
    constructor() {} 
    ngOnInit() {  
    this.onCreate.emit('dummy'); 
    } 

} 

,然後你可以使用它在* ngFor中調用組件中的方法:

<div *ngFor="let item of items"> 
    <div *ngIf="item" (onCreate)="yourMethod(item)"> 
    </div> 
</div> 

在你的組件,你可以調用此方法爲:

yourMethod(item){ 
    console.log(item); 
} 
5

聽起來並不很不錯,但是,最簡單的方法:

<div *ngFor="let item of items"> 
    <span data-dummy="{{myFunction()}}" > here call a function that do something with 'item' and return something new <span> 
</div> 

有道:

@Directive({ 
    selector: '[runDummyFunc]' 
}) 
export class runDummyFunc { 

    constructor() {} 
    ngOnInit() {  
    this.runDummyFunc() 
    } 

} 


<div *ngFor="let item in items"> 
    <span [runDummyFunc]="myFunction" > here call a function that do something with 'item' and return something new <span> 
</div> 

您的課程:

 myFunction =():void=>{ 

     console.log('whatever'); 
     } 
+2

使用插值來調用函數不是一種真實的方式,因爲它會由於'ngOnChanges'多次調用。 @ xe4me –

+1

@BhushanGadekar,我知道這不是一個好方法,這就是爲什麼我說的聽起來不太好,但這是他想要的,他可以很容易地創建一個類似fire_once的變量,並將其設置爲true,如果它是不管怎麼說,這都不是一個好方法 – Milad

4

變化的數據涉及到*ngFor打字稿本身之前,

this.items.forEach((item,index)=>{ 
    item=this.func(item,index); 
}) 

func(item,index):string{ 
    return item+ String(index); //do whatever you wish to do. 
} 
1

模板怎麼不這樣做,你想在組件代碼數據的地方。聽起來就像你想要使用可觀察對象的flatMap之類的東西,它可以讓你爲源可觀察對象中的每個項目返回另一個可觀察對象。這可能是http api調用或任何其他回報(fiddle)

var ids = ["a", "d", "c"]; 
var lookupValues = { "a": 123, "b": 234, "c": 345, "d": 456 }; 

// given an id 
function fakeApiCall(id) { 
    // given an id, return an observable with one entry: an object consisting 
    // of an 'id' property with that id value and a 'lookup' property with the 
    // value from lookupValues for that id 
    return Rx.Observable.just({ id: id, lookup: lookupValues[id] }); 
} 

var o1 = Rx.Observable.from(ids); // 'a, d, c 

var o2 = o1.flatMap(x => { 
    // here we get each value from o1, so we do an api call and return 
    // an observable from each that will have the values in that 
    // observable combined with all others to be sent to o2 
    return fakeApiCall(x); 
}); 

o2.subscribe(x => { 
    document.write(JSON.stringify(x) + "<br/>"); 
}); 

// result: 
// {"id":"a","lookup":123} 
// {"id":"d","lookup":456} 
// {"id":"c","lookup":345}