2017-05-09 52 views
0

appcomponent.html推送數據與標籤(打字稿)陣列

<input type="text" #name><input type="text" #fname> 
<input type="button" value="add" (click)="mymethod(name.value,fname.value)"> 
<ul> 
<li *ngFor="let hero of heroes">{{hero.name}} -- {{hero.fname}}</li> 
</ul> 

appcomponent.ts

import { Component,OnInit } from '@angular/core'; 
import { MyserviceService } from './myservice.service'; 
@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    providers: [MyserviceService], 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent implements OnInit{ 
    heroes=[];  
    constructor(private _myservice:MyserviceService){} 
    ngOnInit(){ 
     this.heroes=this._myservice.heroarr(); 
    } 
    mymethod(name:string,fname:string){ 

    this.heroes.push(name:'hi',fname:'hello'); 

    } 
} 

我已經嘗試過此代碼單個數據推入陣列和它的工作原理well.when我想發送數據與標籤它表明錯誤。如果有人知道請讓我知道。

+0

和錯誤是什麼? – mxr7350

+0

錯誤在/root/project/disdata/src/app/app.component.ts(17,23):','預計。 –

+0

this.heroes.push({name:'hi',fname:'hello'});不是推送對象的正確方法嗎? – XYZ

回答

1

試試這個

let newHero = { 
    name:'hi', 
    fname:'hello' 
} 

this.heroes.push(newHero); 
+0

感謝兄弟它的作品... –