将数据推入带有标签的数组(打字稿)
push data into array with label (typescript)
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我想发送带有标签的数据它表示error.if任何知道的人请告诉我。
试试这个
let newHero = {
name:'hi',
fname:'hello'
}
this.heroes.push(newHero);
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我想发送带有标签的数据它表示error.if任何知道的人请告诉我。
试试这个
let newHero = {
name:'hi',
fname:'hello'
}
this.heroes.push(newHero);