为什么输入文本字段在禁用时不保留其值?

Why doesn't the input text field retain its value when disabled?

提问人:Jinglemahn 提问时间:10/20/2023 最后编辑:Jinglemahn 更新时间:10/22/2023 访问量:43

问:

我正在使用在 StorybookJS 中运行的 Angular 处理一个简单的输入字段。我面临的问题是,当设置为 true 时,输入文本字段没有保留其值。disabled

因此,当我在输入字段中手动键入“hello”文本时,情况如下:enter image description here

当我将属性设置为 时,输入字段被禁用,但没有保留值“hello”,而是显示一个不正确的空文本。disabledtrue

enter image description here

尽管如此,在我上面描述的代码下方。

input.component.html

<input [formControl]="inputValue" />
<div *ngIf="inputValue.invalid && inputValue.touched">
  <small *ngIf="inputValue.errors" class="error-text">This field is required.</small>
</div>

input.component.ts

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

@Component({
  selector: 'app-input',
  templateUrl: './input.component.html',
  styleUrls: ['./input.component.css']
})
export class InputComponent {
  @Input() value: string = '';
  @Input() disabled: boolean = false;
  // Create a FormControl to handle the input value and validation
  inputValue: FormControl = new FormControl();
  constructor() {}
  ngOnInit(): void {
    this.inputValue = new FormControl(
      { value: this.value, disabled: this.disabled },
      [Validators.required]
    );
  }
}

Input.stories.ts

import { InputComponent } from './../../app/input/input.component';
import { Meta, StoryObj } from '@storybook/angular';
import { moduleMetadata } from '@storybook/angular';

import { FormsModule, ReactiveFormsModule } from '@angular/forms';

const meta: Meta<InputComponent> = {
  component: InputComponent,
  title: 'Basic Components/InputComponent',
  tags: ['autodocs'],
  decorators: [
    moduleMetadata({
      declarations: [InputComponent],
      imports: [FormsModule, ReactiveFormsModule],
    }),
  ],
  args: {},
  render: (args: InputComponent) => ({
    props: {
      ...args,
    },
  }),
  argTypes: {},
};
export default meta;
type Story = StoryObj<InputComponent>;

export const Default: Story = {
  args: {
    value: '', 
    disabled: false,
  },
  render: (args) => ({
    props: {
      ...args,
    },
    template: `
        <form>
          <app-input [value]="value" [disabled]="disabled"></app-input>
          <!-- Adding an error message display in the story -->
          <div *ngIf="!value && value !== null">
            <small class="error-text">This field is required.</small>
          </div>
        </form>
      `,
  }),
};

我可以得到一些帮助,为什么输入文本字段在禁用时不保留输入的值?谢谢。

Forms 输入 Storybook Angular-StoryBook

评论


答:

0赞 Ihor Muliar 10/20/2023 #1

使用此代码,首先使用值初始化 FormControl,然后根据需要设置 disabled 属性。这样,当您将 disabled 属性设置为 true 时,输入字段将保留其值:

import { Component, Input, OnInit } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'app-input',
  templateUrl: './input.component.html',
  styleUrls: ['./input.component.css']
})
export class InputComponent implements OnInit {
  @Input() value: string = '';
  @Input() disabled: boolean = false;
  // Create a FormControl to handle the input value and validation
  inputValue: FormControl;

  constructor() {}

  ngOnInit(): void {
    // Initialize the FormControl with the value
    this.inputValue = new FormControl(this.value, [Validators.required]);

    // Set the disabled property as needed
    if (this.disabled) {
      this.inputValue.disable();
    }
  }
}

评论

0赞 Jinglemahn 10/20/2023
我已经尝试过,但问题仍然存在。