【问题标题】:Disable an element with ElementRef使用 ElementRef 禁用元素
【发布时间】:2018-07-23 11:37:23
【问题描述】:

我使用 ElementRef 来禁用一个元素。

//我的viewchild声明

   @ViewChild("txtSysId") txtSysId: ElementRef<HTMLElement, any>;

//我的构造函数注入

  constructor(private _data: GetTransactionService, private formBuilder: FormBuilder, private transDtls: GetTransactionDtlsService, private dateform: FormatDateService, private render: Renderer2, private apiService: ApiService, private login: LoginService, private elementRef: ElementRef<HTMLElement, any>, private changeDetect: ChangeDetectorRef) { }

//禁用代码

this.txtSysId.nativeElement.disabled = false;

Im using visual studio, it gives error on IDE itself as "Property disabled does not exist on HTMLElement", Im 使用 angular6,当尝试从带有断点的控制台执行此操作时,也可以禁用。我是角度新手,请帮助

【问题讨论】:

  • 您要禁用什么?您不能为此使用 [disabled] 或 [attr.disabled] 吗?
  • 我试过 [disabled]="loginSysId!=='Admin'" 它没有t work, even this [disabled]="true" didnt 工作
  • 你最好向我们展示实际的标记。
  • 检查我的问题,它有一个示例如何禁用和启用表单控制元素stackoverflow.com/questions/51443127/…@mano10

标签: angular


【解决方案1】:

使用

this.myForm.get('systemId').disable();

按需禁用该控件。

【讨论】:

    【解决方案2】:

    您似乎在使用响应式表单,因此请充分利用它。

    this.myForm.get('myControl').disable();
    this.myForm.get('myControl').enable();
    

    如果您不想使用它,请发布txtSysId 引用的HTML 代码。

    【讨论】:

    • 好的,但是我的 elementRef 代码有时会在控制台中出错,通用类型 'ElementRef' 需要 0 到 1 个类型参数。
    • 这意味着它要么是ElementRef,要么是ElementRef&lt;HTMLElement&gt;,但不是ElementRef&lt;HTMLElement, any&gt;
    • 如果我使用 ElementRef 或 ElementRef 会显示错误 Generic type element ElementRef requires 2 type arguments
    • 你是从哪里导入的?
    • import { Component, OnInit, Input, ElementRef, Renderer2, AfterViewInit, ViewChild, ChangeDetectorRef } from '@angular/core';
    猜你喜欢
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 2022-07-19
    • 2014-11-28
    相关资源
    最近更新 更多