2017-05-09 55 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

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