简体   繁体   English

Angular 中的字符串插值

[英]String interpolation in Angular

My app.component.ts code:我的app.component.ts代码:

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  isDisabled = false;

  ngOnInit(): void {
    setTimeout(() => {
      this.isDisabled = !this.isDisabled;
    }, 2000);
  }
}

My app.component.html code:我的app.component.html代码:

<input type="text" value="{{ isDisabled }}" disabled="{{ isDisabled }}" />
  • The string interpolation for the disabled attribute is evaluated to a truthy value (thus, making the input disabled from the get-go) disabled属性的字符串插值被评估为真实值(因此,从一开始就禁用输入)
  • The string interpolation for the value attribute is evaluated to false initially and just after the callback of the setTimeout is executed, the value attribute of the input is true . value属性的字符串插值最初被评估为false ,并且在执行setTimeout的回调之后,输入的value属性为true (this is the behavior I expected for the disabled attribute also) (这也是我对disabled属性所期望的行为)

Q: What's causing the difference in the way these two string interpolations work?问:是什么导致这两种字符串插值的工作方式不同?

disabled will disable an element whether it is true or false, it's presence means that the element will be disabled. disabled将禁用一个元素,无论它是真还是假,它的存在意味着该元素将被禁用。
Angular will not add the disabled element at all for [disabled]="variable" if variable is false.如果 variable 为 false,Angular 根本不会为[disabled]="variable"添加禁用元素。

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

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