我正在和Angular15一起工作.我需要帮助,因为我更改了复选框 Select .

根据要求,我有一个菜单栏和复选框.复选框来自可重用组件.此复选框可重复使用菜单栏页面中使用的组件.默认情况下,该复选框处于选中状态.但当我更改菜单栏选项时,应该取消选中该复选框.

在上面的"Display"(显示)页面中,复选框处于选中状态.我想通过更改菜单选项来取消选中该复选框.如果我点击"用户",复选框应该是取消选中的.每次更改菜单选项时,如果复选框处于选中状态,则应将其取消选中.

selectionpage.component.html

<p>Check Box Selection!</p>

<input type="checkbox" [(ngModel)]="isChecked">Select All

添加了一个输入参数为"islogic"的复选框.

selectionpage.component.ts

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

@Component({
  selector: 'app-selectionpage',
  templateUrl: './selectionpage.component.html',
  styleUrls: ['./selectionpage.component.scss']
})
export class SelectionpageComponent {
@Input() isChecked:boolean=true;
}

现在,我将使用"DisplayPage"中的"SelectionPage"来添加复选框.

display-page.component.html

<div class="tab-menu-container">
  <div class="tab-menu-wrapper">
    <ng-container *ngFor="let selectvalue of selectionContent">
      <div class="tab">
        <span (click)="changerArchiveType()" style="cursor: pointer;">{{ selectvalue }}</span>|
      </div>
    </ng-container>
  </div>
</div>
<app-selectionpage [isChecked]="SelectionChange"></app-selectionpage>

display-page.component.scss

.tab-menu-container{
    height: 56px;
    display:flex;
    justify-content: center;
    background: #f2f2f2;
    .tab-menu-wrapper{
        width: 85%;
        display: flex;
        flex-wrap: wrap;
        column-gap: 2rem;
        padding-top: 20px;

        .tab{
            width: fit-content;
            font-weight: 700;
            font-size: 15px;
            line-height: 20px;
        }
    }
}

display-page.component.ts

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

@Component({
  selector: 'app-display-page',
  templateUrl: './display-page.component.html',
  styleUrls: ['./display-page.component.scss'],
})
export class DisplayPageComponent {
  public selectionContent: Array<string> = [
    'User',
    'Admin',
    'SuperAdmin',
    'Seller',
  ];
  public SelectionChange:boolean=false;
  changerArchiveType() {
    this.SelectionChange=false;
    console.log("Checked:",this.SelectionChange)
  }
}

现在,页面上出现了带有选项和复选框的"菜单栏".

如果我更改了菜单栏选项,复选框应该是取消选中的.它是在页面加载后第一次发生的.但在此之后,通过更改菜单来取消选中不起作用.

谢谢你

推荐答案

我很惊讶这能行得通.原因是DisplayPageComponent中的SelectionChange成员变量从未改变其初始值false.

每当使用EventEmitterOutput修饰符选中复选框时,您都需要发出更改.按照惯例,如果您将输出变量命名为与输入变量相同的名称,并且后缀为Change,则可以使用"框中的香蕉绑定",就像使用ngModel指令一样.

selectionpage.component.ts

添加isCheckedChange事件emits 器.

export class SelectionpageComponent {
  @Input() isChecked: boolean = true;

  @Output() readonly isCheckedChange = new EventEmitter<boolean>();
}

selectionpage.component.html

ngModel发生变化时,发出isCheckedChange事件.

<input type="checkbox" [(ngModel)]="isChecked" 
       (ngModelChange)="isCheckedChange.emit($event)">Select All

display-page.component.html

外部组件中的isChecked使用双向绑定.

<app-selectionpage [(isChecked)]="SelectionChange"></app-selectionpage>

Typescript相关问答推荐

Tailwind CSS样式不在Svelte应用程序中呈现

界面中这种类型的界面界面中的多态性

在React中实现具有独立页面的嵌套路径的最佳实践是什么?

如何在方法中定义TypeScript返回类型,以根据参数化类型推断变量的存在?

用泛型类型覆盖父类函数导致类型y中的Property x不能赋给基类型Parent中的相同属性."'''''' "

泛型函数类型验证

如何使类型只能有来自另一个类型的键,但键可以有一个新值,新键应该抛出一个错误

单击并移除for循环中的一项也会影响另一项

使用泛型keyof索引类型以在if-condition内类型推断

路由链接不会导航到指定router-outlet 的延迟加载模块

如何表示多层深度的泛型类型数组?

如何在Angular 12中创建DisplayBlock组件?

如果字符串文字类型是泛型类型,则用反引号将该类型括起来会中断

TypeScrip:强制使用动态密钥

创建依赖函数参数的类型

字符串文字联合的分布式条件类型

有没有办法在Reaction组件中动态导入打字脚本`type`?

内联类型断言的工作原理类似于TypeScrip中的断言函数?

React-跨组件共享功能的最佳实践

如何按成员资格排除或 Select 元组?