Angular 6: How to build a simple multiple checkbox to be checked/unchecked by the user?

前端 未结 3 416
不思量自难忘°
不思量自难忘° 2021-01-02 16:11

I am writing this post after having read several threads concerning this topic but no one of them gives me what I need. This post seems to have the solution but I do not hav

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-02 16:42

    Your countries like

    {id: 1, name: 'Italia',checked:false},
    

    Your html like

    You'll get an array like, e.g.

     [{id: 1, name: 'Italia',checked:false},{id: 2, name: 'Brasile',checked:tue}..]
    

    you can do

    result=this.countries.filter(x=>x.checked).map(x=>x.id)
    //result becomes [2,...]
    

提交回复
热议问题