1

I am struggling to get AngularJS to work properly with a small thing that I am trying to implement.

I have an input of type number (which likely causes the issue) which is bound to a variable in my controller, and I need to disable a button if this input is empty.

Here's the code for my input and button:

<input type="number" min="0" max="20" ng-model="student.mark">

<button ng-disabled="!student">Send</button>

I tried several solutions to disable my button if the field is empty, but enable it if it has a value of 0:

<button ng-disabled="!student || !student.mark">Send</button>

<button ng-disabled="!student || !angular.isNumber(student.mark)">Send</button>

<button ng-disabled="!student || student.mark == ''">Send</button>

<button ng-disabled="!student || student.mark === ''">Send</button>

None of those seem to work. It will work properly if I have any number in my input, but either the button will be disabled if I have the value "0", or the button will stay enabled if I put a valid value and then remove it.

What makes me think that it should work is the fact that if I display the value using {{ student.mark }}, the value 0 will indeed show as "0" and an empty input will show as empty.

Thanks.

6
  • perhaps you have more logic behind the scenes, but it works if you have just ng-disabled="!student.mark" (maybe you need && instead of ||) Commented Jan 10, 2018 at 11:19
  • @AlekseySolovey With this solution the button is disabled when the value of the field is "0" while it should be enabled. Commented Jan 10, 2018 at 11:22
  • Put a required attribute on the input? Commented Jan 10, 2018 at 11:33
  • @Endless Yeah but it won't disable my button. Commented Jan 10, 2018 at 11:36
  • 1
    IMHO i think disabling a submit btn is a bad practice. Having a good constrain validation on the form gives meningsfull errors to the form, you don't get them if you can't click the submit btn Commented Jan 10, 2018 at 11:38

3 Answers 3

1

NOTE : 0 is treated as false.

In your controller define

 $scope.student.mark = null;

Use condition

 ng-disabled="student.mark == null && student.mark < 0"
Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest you to use form for that, something like that:

<form name="formName" ng-submit="someMethod(student)" autocomplete="off" 
novalidate>

<input type="number" min="0" max="20"
    ng-model="student.mark" placeholder="e.g. 12"
    ng-required="true" /> 
<button type="submit" value="Send" ng-disabled="formName.$invalid || <something else here>" />
</form>

2 Comments

I cannot really apply that solution as I have a form (without the form tag) in each row of a table, which makes it not valid HTML if I add the form tag. Not great but yeah...
bummer :/, I usually prefer to use form in this cases. maybe next time it will helps you :)
0

I tried a few more things and actually testing if the value is null did the trick:

<button ng-disabled="!student || student.mark === null">Send</button>

Comments

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.