0

I have this requirement where i need to disable a button based on some condition:

Controller:

<div  ng-controller= "myController" class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"   aria-hidden="true">×</button>
                    <h3 class="modal-title"> Add</h4>

I need to disable this button when I have added 5 entries in html page.Can someone help me out?

4 Answers 4

3

If you have an array of that items, assume that the array is called items, you can pass a condition to the ng-disabled to set to true if the items.length >= 5.

<button type="button" class="close" ng-disabled="items.length >= 5" data-dismiss="modal" aria-hidden="true" >×</button>
Sign up to request clarification or add additional context in comments.

2 Comments

Can i call a function within ng-disabled?
Yes you can. It gets an expression inside
0

When Entries Length is 5

<button ng-disabled="entries.length == 5"></button>

3 Comments

what is the difference between your answer and the answer below?
Where is your answer @jitender
Bro I don't answer just see answer given by @suren below 5 min ago than your answer
0

if your value or the entires is array then you can do as follows

    <div  ng-controller= "myController" class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"   aria-hidden="true">×</button>
                    <h3 class="modal-title"> Add</h4>
<button ng-disabled="entries.length >= 5">ADD</button>

3 Comments

what is the difference between your answer and the answer below?
I dont see your answer anywhere @jitender
am never say answer by me i said answer below check it out stackoverflow.com/a/46461100/5621827
0

Try using this

    <div ng-controller="myController" class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true" ng-disabled="isButtonDisabled()">×</button>
                <h3 class="modal-title"> Add</h3>
            </div>
        </div>
    </div>

And at controller

$scope.isButtonDisabled = function() {
    return $scope.requiredArray.length >= 5;
}

This function will check whether the length of the required array is greater than 5 or not. If true button will be disabled.

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.