MariaJen
MariaJen

Reputation: 1143

Disable button in angular with two conditions?

Is this possible in angular ?

<button type="submit" [disabled]="!validate && !SAForm.valid">Add</button>

I expect that if both of the conditions are true they will enable the button.

I've already tried the above code but it's not working as expected.

Upvotes: 91

Views: 299474

Answers (8)

itty
itty

Reputation: 35

I noticed that in the disabled button there is no option at all to do the operator & there is only an option to do || (or) and it works like && Exactly the same.

Upvotes: 0

Tsvetoslav Tsvetkov
Tsvetoslav Tsvetkov

Reputation: 1176

Button and any other elements can be disabled in the Form based on conditional param defined in the component. For example:

In the component(typescript) define the boolean field:

isTheElementDisabled: boolean;

In the template as an element attribute:

[disabled]="isTheElementDisabled"

Upvotes: 0

zaza
zaza

Reputation: 31

I use the ternary operator, but compare if form.valid is true. Ej:

<button mat-button color="primary" [disabled]="((formUser.valid==true) && (formColaborador.valid==true))? false:true" (click)="addClick()">Crear</button>

Upvotes: 3

Sarjerao Ghadage
Sarjerao Ghadage

Reputation: 1544

Using the ternary operator is possible like following.[disabled] internally required true or false for its operation.

<button type="button" 
  [disabled]="(testVariable1 != 0 || testVariable2!=0)? true:false"
  mat-button>Button</button>

Upvotes: -2

bvdb
bvdb

Reputation: 24770

In addition to the other answer, I would like to point out that this reasoning is also known as the De Morgan's law. It's actually more about mathematics than programming, but it is so fundamental that every programmer should know about it.

Your problem started like this:

enabled  = A and B
disabled = not ( A and B )

So far so good, but you went one step further and tried to remove the braces. And that's a little tricky, because you have to replace the and/&& with an or/||.

not ( A and B ) = not(A) OR not(B)

Or in a more mathematical notation:

enter image description here

I always keep this law in mind whenever I simplify conditions or work with probabilities.

Upvotes: 28

Vishal Hulawale
Vishal Hulawale

Reputation: 522

Declare a variable in component.ts and initialize it to some value

 buttonDisabled: boolean;

  ngOnInit() {
    this.buttonDisabled = false;
  }

Now in .html or in the template, you can put following code:

<button disabled="{{buttonDisabled}}"> Click Me </button>

Now you can enable/disable button by changing value of buttonDisabled variable.

Upvotes: 15

Amr Eladawy
Amr Eladawy

Reputation: 4358

Is this possible in angular 2?

Yes, it is possible.

If both of the conditions are true, will they enable the button?

No, if they are true, then the button will be disabled. disabled="true".

I try the above code but it's not working well

What did you expect? the button will be disabled when valid is false and the angular formGroup, SAForm is not valid.

A recommendation here as well, Please make the button of type button not a submit because this may cause the whole form to submit and you would need to use invalidate and listen to (ngSubmit).

Upvotes: 0

DeborahK
DeborahK

Reputation: 60596

It sounds like you need an OR instead:

<button type="submit" [disabled]="!validate || !SAForm.valid">Add</button>

This will disable the button if not validate or if not SAForm.valid.

Upvotes: 193

Related Questions