简体   繁体   English

如何在 angular 2 中禁用 div 标签

[英]How to disable the div tag in angular 2

I am trying to disable the div tag after a success callback of that action.我试图在该操作成功回调后禁用 div 标签。

please look my ion-content请看我的离子含量

<ion-content padding class="forgot-password">
  <div [ngClass]="{active: isOn,disabled: isDisabled}">
    <ion-item>
        <ion-label floating>Email/Mobile</ion-label>
        <ion-input type="text" [(ngModel)]="loginId"></ion-input>
    </ion-item> <br><br>

    <button class="float-right" (click)="generateOTP(!isOn);">Send OTP</button><br><br><br>
  </div>
  <br>

  <div *ngIf="showRePasswd">
    <ion-item>
        <ion-label floating>Enter OTP</ion-label>
        <ion-input type="text" [(ngModel)]="passwd"></ion-input>
    </ion-item> <br><br>

    <button class="float-right" (click)="resetPassword();">Send Password</button>
  </div>
</ion-content>

here is my.ts file这是 my.ts 文件

export class ForgotPasswordPage {

    public loginId = "";
    public passwd = "";

  public showRePasswd = false;
  isDisabled = false;
  isOn = false;

  constructor(private navCtrl: NavController, private logger: Logger, private user: Users) {

  }

  generateOTP(newstate) {
    this.logger.info("invoking generateOTP FN");
    var _this = this;
    this.user.generateOTP(this.loginId, function(result,data){
      if(result == '1') {
        alert(data);
        _this.showRePasswd = !_this.showRePasswd;
        _this.isDisabled = true;
        _this.isOn = newstate;
      }
      else {
        //this.showRePasswd = this.showRePasswd;
        alert(data);
      }
    })
  }

  resetPassword() {
    this.logger.info("invoking resetPassword FN");
    var _this = this;

    this.user.resetPassword(this.passwd, function(result,data) {
      if(result == '1') {
        alert(data);
        _this.navCtrl.push(LoginPage);
      }
      else {
        alert(data);
      }
    })
  }
}

I tried [ngClass] but i am not able to make my div tag disable after the success callback.我尝试[ngClass]但我无法在成功回调后禁用我的 div 标签。

I also tried using [disabled] but not working我也尝试使用[disabled]但不工作

Here is a demo for disable a div tag but in my case not working这是禁用 div 标签的演示,但在我的情况下不起作用

My requirement is to make my input field and button to be disabled after success callback我的要求是在成功回调后禁用我的输入字段和按钮

You can add the attribute like您可以添加属性,如

<div [attr.disabled]="isDisabled ? true : null">

but <div> doesn't support the disabled attribute.<div>不支持disabled属性。

Perhaps this is what you want也许这就是你想要的

<div (click)="isDisabled ? $event.stopPropagation() : myClickHandler($event); isDisabled ? false : null"
   [class.isDisabled]="isDisabled"></div>

with some custom CSS that makes .isDiabled look disabled.使用一些自定义 CSS 使.isDiabled看起来被禁用。

It might be better to create a method to put the code there instead of inline.最好创建一个方法将代码放在那里而不是内联。

You can use CSS property pointer-events: none;您可以使用 CSS 属性pointer-events: none; in your tag div:在您的标签 div 中:

<ion-content padding class="forgot-password">
  <div style="pointer-events: none;">
   ...
  </div>
</ion-content>

This is used in my Angular 8 project: First set to HTML file like below.这在我的 Angular 8 项目中使用:首先设置为如下所示的 HTML 文件。 Set to Div tag ngClass=>设置为 Div 标签 ngClass=>

<div class="col-md-3" [ngClass]="{disabledNoOfCasesDiv: !isActiveNOofCasesNo}>
<label class="control-label mb-2">No. of Cases Receive</label>
<input type="number" class="form-control" [(ngModel)]="CollectJob.NoOfCases"
placeholder="No. Cases Receive" name="NoCasesReceive">
</div>

Then next step write CSS for disabling events:然后下一步编写 CSS 以禁用事件:

.disabledNoOfCasesDiv{ pointer-events: none; opacity: 2.0;}

Then last:然后最后:

declare the variable and set to boolean声明变量并设置为布尔值

 isActiveNOofCasesNo: boolean;

Then next just pass true/false values wherever you want to enable div tag or disable div tag, div will automatically enabled or disabled.然后接下来只需在您想要启用 div 标签或禁用 div 标签的任何地方传递 true/false 值,div 将自动启用或禁用。

this.isActiveNOofCasesNo = true;
this.isActiveNOofCasesNo = false;

Thank You..... Happy Learning!...:) 谢谢.....快乐学习!...:)

You Can easily enable/disable the DOM elements using the Angular Directives :-您可以使用 Angular 指令轻松启用/禁用 DOM 元素:-

Create a Simple Directive - DisableDirective创建一个简单的指令 - DisableDirective

import { AfterViewInit, Directive, ElementRef, Input, OnChanges, Renderer2 } from '@angular/core';

const DISABLED = 'disabled';
const APP_DISABLED = 'app-disabled';
const TAB_INDEX = 'tabindex';
const TAG_ANCHOR = 'a';

@Directive({
  selector: '[appDisable]'
})
export class DisableDirective implements OnChanges, AfterViewInit {

  @Input() appDisable = true;

  constructor(private eleRef: ElementRef, private renderer: Renderer2) { }

  ngOnChanges() {
    this.disableElement(this.eleRef.nativeElement);
  }

  ngAfterViewInit() {
    this.disableElement(this.eleRef.nativeElement);
  }

  private disableElement(element: any) {
    if (this.appDisable) {
      if (!element.hasAttribute(DISABLED)) {
        this.renderer.setAttribute(element, APP_DISABLED, '');
        this.renderer.setAttribute(element, DISABLED, 'true');

        // disabling anchor tab keyboard event
        if (element.tagName.toLowerCase() === TAG_ANCHOR) {
          this.renderer.setAttribute(element, TAB_INDEX, '-1');
        }
      }
    } else {
      if (element.hasAttribute(APP_DISABLED)) {
        if (element.getAttribute('disabled') !== '') {
          element.removeAttribute(DISABLED);
        }
        element.removeAttribute(APP_DISABLED);
        if (element.tagName.toLowerCase() === TAG_ANCHOR) {
          element.removeAttribute(TAB_INDEX);
        }
      }
    }
    if (element.children) {
      for (let ele of element.children) {
        this.disableElement(ele);
      }
    }
  }
}

Now Use this directive with your component div :-现在将此指令与您的组件 div 一起使用:-

<div [appDisable]="true">
</div>

Note - Not to forget register Directive in your AppModule.注意 - 不要忘记在您的 AppModule 中注册指令。

Please Refer the POST for the detailed discription.请参阅POST以获取详细说明。

in html在 html

<div[appViewDisable]="disable_condition"> <div[appViewDisable]="disable_condition">

in.ts in.ts

disable_condition = True or False disable_condition = True 或 False

True will disable the content init. True 将禁用内容初始化。

Div element cannot be disabled like form controls. Div 元素不能像表单控件一样被禁用。 You can disable form controls in div instead.您可以改为禁用 div 中的表单控件。

Provided example has custom class "disabled"提供的示例具有自定义类“禁用”

styles: [`
    .button {
      width: 120px;
      border: medium solid black;
    }

    .active {
      background-color: red;
    }

    .disabled {
      color: gray;
      border: medium solid gray;
    }
  `] 

HTML file HTML 文件

div [ngClass]="condition"

ts file ts文件

this.condition = 'mydisable';

css file css文件

.mydisable {
  pointer-events: none;
}

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

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