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');
}
}
我已尝试使用此代码将单个数据推送到数组中并且运行良好。当我想发送带有标签的数据时,它表示错误。如果有人知道请告诉我。
答案 0 :(得分:1)
试试这个
let newHero = {
name:'hi',
fname:'hello'
}
this.heroes.push(newHero);