正确使用Angular / typsecript用选定的对象填充数组

Iv有一个有角度的应用程序,它从spring-boot rest api(简单播放器对象的集合-id,name,position,value ...的集合)中读取json资源...。我有一个按钮,其中每个简单显示的对象都位于UI上,而我只是想在用户单击component.html上的“购买”按钮时将每个对象存储在数组中。...我需要使用我的component.ts文件,而我是TypeScript的新手,不确定如何链接html方法调用.ts逻辑页面...

任何帮助,不胜感激的家伙。...这只是我基本的幻想足球类型应用程序的第二阶段,其中的列表(如果我在Java中,则为arraylist)将代表用户选择购买的玩家/他们的自定义团队... ..

COMPONENT.HTML:

     <ul *ngIf="Ballers">

      <li *ngFor="let player of Ballers">
        <p class="name">{{player.name}}</p>
        <p class="position">{{player.position}}</p>
        <p class="value">£{{player.value}} m</p>
        <button class="buyButton" on-click="buyThisPlayer()">Add to Squad</button>
     </li>
   </ul>
  </div>

COMPONENT.TS:

 @Component({
    selector: 'app-player-list',templateUrl: './playerList.component.html',styleUrls: ['./playerList.component.scss']
 })
export class PlayerListComponent implements OnInit {

  Ballers: Object;
 // let selectedKickers=[] ;   //to declare my empty array???

   constructor(private httpService: HttpService) { }

  ngOnInit() {
      this.httpService.getPlayers().subscribe(data => {
        this.Ballers = data;
        console.log(this.Ballers);
      })
  }

  buyThisPlayer(){
   selectedKickers.push(this); // this seems the only way to pinpoint my object to add???
  }

}

Z584036976 回答:正确使用Angular / typsecript用选定的对象填充数组

如果您只对点击处理程序感兴趣,则可以在方法中传递播放器并像这样绑定它:

$ curl -H "Authorization: bearer $private_token" https://api.github.com/repos/dmytrohoi/site.source/contents/

[
  {
    "name": ".github","path": ".github","sha": "hash","size": 0,"url": "https://api.github.com/repos/dmytrohoi/site.source/contents/.github?ref=master","html_url": "https://github.com/dmytrohoi/site.source/tree/master/.github","git_url": "https://api.github.com/repos/dmytrohoi/site.source/git/trees/hash","download_url": null,"type": "dir","_links": {
      "self": "https://api.github.com/repos/dmytrohoi/site.source/contents/.github?ref=master","git": "https://api.github.com/repos/dmytrohoi/site.source/git/trees/hash","html": "https://github.com/dmytrohoi/site.source/tree/master/.github"
    }
  },...
]
<button class="buyButton" (click)="buyThisPlayer(player)">Add to Squad</button>

我还建议其他建议(更深入地探讨最佳实践)。让我知道您是否有兴趣。

本文链接:https://www.f2er.com/3155736.html

大家都在问