4

I need to enable / disable the below button and I have the below code. It works fine only from second time onwards. Initially when I land on the page, the button should be disabled because I dont have a value in the input box. If i enter something inside the text box and delete it , the button gets disabled. How do I disable the button for the first when I land on the page in angular 4

<input type="email" class="form-control" id="inputValue"  placeholder="[email protected]" formControlName="inputValue">
 <button type="button" id="verifyBtn" [disabled]="inputValue === ''" class="btn btn-primary btn-large" (click)="verify()">Verify</button>
4
  • Sidenote... do not use formcontrol and ngmodel together. It will be deprecated soon anyway. And from v6, this would also give you a warning. Commented May 25, 2018 at 5:30
  • Ahh okay.. SO how do i achieve this without ngModel Commented May 25, 2018 at 5:31
  • You would set the initial value for the formcontrol instead of ngModel. But in this case you wouldn't want an intial value :) Pleae show the component code. Commented May 25, 2018 at 5:34
  • @AJT_82 I have lots of code in component file. Which part of component code you want to see? Commented May 28, 2018 at 8:51

2 Answers 2

11

You can set input like this:

[disabled]="!inputValue"

Because the inputValue value is not set at the beginning, which is undefined, so when you check inputValue === '' it will be false

[Update based on comment]
If you use [(ngModel)], which is bi-directional, you need to be careful about the data input from view to controller, and output from controller to view. In many times, It is a bad design.

Because you use fromControlName, which is reactiveForm, you should just use reactiveForm and subscription to handle input value. You can check the post

Sign up to request clarification or add additional context in comments.

5 Comments

Excellent solution!1
I think there is a problem with because of both ngModel and formControlName . With ngModel , I am getting the value of inputValue field as undefined in ts file. (this.personalDtlsForm.value.inputValue !== ''") If i remove that ngModel , the value is getting passed as "" . As a result of this I had to remove the ngModel code. Is there any way to achieve this without using ngModel ?
But where is the solution ? How do i disable the button without using ngModel and with formControlName
When using reactive forms, you can use the form validation, and using the validation result to control whether to disable or show error.
That is for form validation . I was asking to disable a single button that is present inside the form
2

Based on you updated question you want to disable button control when you land on page , to do that you should create your form control like as below

this.heroForm = this.fb.group({
  inputValue : ['', Validators.required ],
});

if you do as above then you control will enter in invalidate state first time itself. in your button html you should do this

<form [formGroup]="heroForm">
    <button
     type="button"
     id="verifyBtn"
     formControlName="inputValue"
     [disabled]="heroForm.get('inputValue').invlaid"
     class="btn btn-primary btn-large"
     (click)="verify()">Verify</button>
  </form>

can you try like this , so code below checks inputvalue is there or not and if inputvalue is there check that its has value or not

[disabled]="!inputValue || (inputValue && inputValue.length < 1)"

9 Comments

Great solution !!
I am facing the issue with this. I have mentioned by comment in the above answer
@Nancy - i dont see comment on my answer ...where you commented
I think there is a problem with because of both ngModel and formControlName . With ngModel , I am getting the value of inputValue field as undefined in ts file. (this.personalDtlsForm.value.inputValue !== ''") If i remove that ngModel , the value is getting passed as "" . As a result of this I had to remove the ngModel code. Is there any way to achieve this without using ngModel ?
@Nancy - you cannot use both Template drive from approch and Rective form approach, you have to use one of them ...if you can update question i can add my answer according to it ....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.