您如何在“*ngfor”循环中呼吁单击复选框的值?

发布于 2025-02-03 07:29:17 字数 1192 浏览 1 评论 0原文

我试图将“单击”项目的值调用为 *ngfor循环中的一个。到目前为止,这是我的代码:

html:

<form [formGroup]='skillChoice'>
...

  <li *ngFor='let skill of skills'>
    <mat-checkbox
      [disabled]='enableSkill(skill)'
      [formControlName]='skill'
      (click)='onClick(skill)'
      >
       {{ skill }}
    </mat-checkbox>
  </li>

ts:

  ngOnInit(): void{
    this.skillChoice.valueChanges.subscribe(val => {
             // val = {skill1: true, skill2: false, skill3:false...}
      ... //other various code
      let skillTracker = new Array()
        // skillTracker needs to be an array of recently clicked items, 
        // putting them in order of when they were clicked, not 
        // alphabetically.
           // i.e. skillTracker = [skill1, skill5, skill3] 
           //  if skill1 is clicked first, and skill3 is clicked last
    }
  }
...
  onClick(skill: string) {
    return skill
  }

在我的研究中,我发现使用this.skillchoice.get(“&lt;在此处插入技能名称”)会读取Boolean值技能。但这不是我想要的。我希望它可以阅读并打印val数组的“技能”键。有没有办法调用在valuechanges.subscribe中单击的“事件”,而无需为循环创建val中的技能循环?

I'm trying to call the value of the "clicked" item for a that is in an *ngFor loop. So far, this is my code:

HTML:

<form [formGroup]='skillChoice'>
...

  <li *ngFor='let skill of skills'>
    <mat-checkbox
      [disabled]='enableSkill(skill)'
      [formControlName]='skill'
      (click)='onClick(skill)'
      >
       {{ skill }}
    </mat-checkbox>
  </li>

TS:

  ngOnInit(): void{
    this.skillChoice.valueChanges.subscribe(val => {
             // val = {skill1: true, skill2: false, skill3:false...}
      ... //other various code
      let skillTracker = new Array()
        // skillTracker needs to be an array of recently clicked items, 
        // putting them in order of when they were clicked, not 
        // alphabetically.
           // i.e. skillTracker = [skill1, skill5, skill3] 
           //  if skill1 is clicked first, and skill3 is clicked last
    }
  }
...
  onClick(skill: string) {
    return skill
  }

In my research, I found that using this.skillChoice.get("<insert skill name here>") would read the boolean value of the skill. But that's not what I want. I want it to read and print the "skill" key of the val array. Is there a way to call the "event" of clicking within the valueChanges.subscribe without creating a for loop on the skills in val?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文