[英]how to hide/show html tag in type script in Angular component
I have created a form in angular Html component, which is used to add users and update them using dialog module.我在 angular Html 组件中创建了一个表单,用于添加用户并使用对话框模块更新它们。 If the user clicks for updating, I want to hide password input in HTML.
如果用户点击更新,我想在 HTML 中隐藏密码输入。
<mat-form-field appearance="outline">
<mat-label>Password</mat-label>
<input formControlName="password" matInput/>
</mat-form-field>
You can use *ngIf
directive to show/hide content.您可以使用
*ngIf
指令来显示/隐藏内容。 Basically, it accepts an expression.基本上,它接受一个表达式。 When the expression evaluates to
true
it shows the DOM, otherwise, it removes it from DOM.当表达式计算结果为
true
时,它会显示 DOM,否则,它会将其从 DOM 中删除。 Over here you can control the behavior using isUpdating
boolean.在这里,您可以使用
isUpdating
boolean 控制行为。
<mat-form-field appearance="outline" *ngIf="isUpdating">
<mat-label>Mot de passe</mat-label>
<input formControlName="password" matInput/>
</mat-form-field>
I feel that for this use case, rather than hiding the field.我觉得对于这个用例,而不是隐藏字段。 You can disable the field until it is updating the
password
field.您可以禁用该字段,直到它更新
password
字段。 By show/hide field would introduce a flicker effect on UI (and that would be annoying for user)通过显示/隐藏字段会在 UI 上引入闪烁效果(这对用户来说很烦人)
<mat-form-field appearance="outline" *ngIf="isUpdating">
<mat-label>Mot de passe</mat-label>
<input formControlName="password" matInput [disabled]="isUpdating"/>
</mat-form-field>
After disabling the field.禁用该字段后。 You can not retrieve
password
field from FormGroup
object.您无法从
FormGroup
object 中检索password
字段。 You can get that using form.getRawValue()
method instead of form.value
method.您可以使用
form.getRawValue()
方法而不是form.value
方法来获得它。
component.html组件.html
<ng-container *ngIf='!isUpdating'>
<mat-form-field appearance="outline">
<mat-label>Mot de passe</mat-label>
<input formControlName="password" matInput/>
</mat-form-field>
</ng-container>
component.ts组件.ts
export class Component {
isUpdating = false;
onUpdate() {
this.isUpdating = true;
}
}
You can add condition on Password Input Whenever isUpdating will set to true then password input will removed from DOM and If isUpdating will set to false then Password input will add in DOM automatically.您可以在密码输入上添加条件每当isUpdating设置为 true 时,密码输入将从 DOM 中删除,如果isUpdating设置为 false,则密码输入将自动添加到 DOM 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.