2017-06-25 22 views
0

我對angular2沒有任何知識,但是試圖將功能添加到現有代碼中。組件中帶參數的調用函數

這是組件函數調用:

<a (click)="somefunction()">TEST</a> 

我想和參數來調用它。怎麼做?因爲像這樣傳遞不起作用:(click)="somefunction(something='test')

回答

0

當您調用函數時,您不需要指定變量,因爲應該在javascript中處理輸入。以下代碼假定您使用的是位於同一組件中的ts和html。

我很快就組裝了一個組件來幫助給你一些背景。

HTML

 <tr *ngFor="let staff of allStaff"> 
      <td>{{staff.name}}</td> 
      <td>{{staff.position}}</td> 
      <td>{{staff.created_at}}</td> 
      <td><button (click)="here(staff)"><span>View</span></button> 
     </tr> 

TS

import { Component, AfterViewChecked } from '@angular/core'; 
import { StaffService } from '../staff.service'; 

import { Staff } from '../staff'; 

@Component({ 
    templateUrl: './view-all.component.html' 
}) 

export class ViewAllComponent { 
    allStaff: Staff[]; 

    constructor(
    private staffService: StaffService, 
) { 
    this.getAllStaff(); 
    }; 

    getAllStaff(): void { 
    this.staffService.getAllStaff().then(res => this.allStaff = res); 
    }; 

    here(staff: Staff): void { 
    console.log(staff); 
    } 
}