简体   繁体   English

Angular - boolean 值不变

[英]Angular - boolean value doesn't change

I'm facing issue with my app.我的应用程序遇到问题。 I'm trying to create a list of checkboxes, when the checkbox is clicked the value should be true and when clicked again the value should be false.我正在尝试创建一个复选框列表,当单击复选框时,该值应该为 true,再次单击时,该值应该为 false。 Simple.简单的。

Here is my html这是我的 html

  <li *ngFor="let checkbox of checkboxSelect">
      <div class="checkbox-style">
          <input type="checkbox" name ="{{checkbox.inputName}}" 
                [checked]="checkbox.name"
                (change)="checkbox.name= !checkbox.name"/>
             <div class="state">
                 <label>{{checkbox.label}}</label>
             </div>
      </div>
  </li>

Here is my component.ts这是我的 component.ts

showFirstName = true;
showLastName = true;

checkboxSelect: any[] = [{
        label: 'First name',
        name: this.showFirstName,
        inputName: 'showFirstName'
    }, {
        label: 'Last name',
        name: this.showLastName,
        inputName: 'showLastName'

    },
];

Everything is working except my 'name' value doesn't change to true/false when clicked.一切正常,除了我的“名称”值在单击时不会更改为真/假。

On the other hand when I put that straight to html without *ngFor it's working另一方面,当我把它直接放到 html 没有 *ngFor 它工作

  <li>
      <div class="checkbox-style">
          <input type="checkbox" name = "firstName"
                [checked]="showFirstName"
                (change)="showFirstName = !showFirstName"/>
             <div class="state">
                 <label>First Name</label>
             </div>
      </div>
  </li>

I wasn't so clear, my goal is to display divs depend on true/false我不是很清楚,我的目标是显示 div 取决于真/假

 <div *ngIf="showFirstName" scope="col">First name</div>
<div *ngIf="showLastName" scope="col">Second name</div>

But it doesn't change at all.但它根本没有改变。

Does anyone have an idea what's wrong with my code?有谁知道我的代码有什么问题?

Based on your comment, you are trying to use variables showFirstName and showLastName that aren't modified by the event handler to show/hide strings First Name and Last Name .根据您的评论,您正在尝试使用未由事件处理程序修改的变量showFirstNameshowLastName来显示/隐藏字符串First NameLast Name To make the value binding persist between two variables, you could make them objects.要使值绑定在两个变量之间持续存在,您可以将它们设为对象。 Objects are usually passed by reference in Javascript.对象通常在 Javascript 中通过引用传递。 Try the following尝试以下

Controller Controller

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  showFirstName = { status: true };
  showLastName = { status: true };

  checkboxSelect: any[] = [{
    label: 'First name',
    name: this.showFirstName,
    inputName: 'showFirstName'
  }, {
    label: 'Last name',
    name: this.showLastName,
    inputName: 'showLastName'

  }, ];

  checkStatus() {
    console.log(this.checkboxSelect[0].name, this.checkboxSelect[1].name);
  }
}

Template模板

<li *ngFor="let checkbox of checkboxSelect">
  <div class="checkbox-style">
    <input type="checkbox" name="{{checkbox.inputName}}" [checked]="checkbox.name.status"
      (change)="checkbox.name.status = !checkbox.name.status; checkStatus()" />
    <div class="state">
      <label>{{checkbox.label}}</label>
    </div>
  </div>
</li>

<table>
  <th *ngIf="showFirstName.status" scope="col">First Name</th>
  <th *ngIf="showLastName.status" scope="col">Last Name</th>
</table>

Note: Using object reference to control the state of variables could get very tangled later in development.注意:使用 object 参考来控制变量的 state 在开发后期可能会变得非常复杂。 Better way would be to think of a different structure.更好的方法是考虑不同的结构。

Working example: Stackblitz工作示例: Stackblitz

Change your html to:-将您的 html 更改为:-

<li *ngFor="let checkbox of checkboxSelect">
      <div class="checkbox-style">
          <input type="checkbox" [name] ="checkbox.inputName" 
                [checked]="checkbox.name"
                (change)="checkbox.name = !checkbox.name"/>
             <div class="state">
                 <label>{{checkbox.label}}</label>
             </div>
      </div>
  </li>

Why don't you use Angular ngModel?为什么不使用 Angular ngModel? Then you don't need to define change handler.然后你不需要定义更改处理程序。

<li *ngFor="let checkbox of checkboxSelect">
      <div class="checkbox-style">
          <input type="checkbox" name ="{{checkbox.inputName}}" 
                [(ngModel)] ="checkbox.name"/>
             <div class="state">
                 <label>{{checkbox.label}}</label>
             </div>
      </div>
  </li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM