0

Quite simple just not too sure how to code it, I have an input box which sets a value, it then submits the value for processing, Currently I want the value to default to 0 in the input box, if it is currently not assigned a value, i.e 10.

<div class="row">
    <div class="col-sm-8">
    <label for="totalStaff" style="padding-bottom: 10px; width: 150px;">Number of Staff</label>
    <button class="form-button" ng-click="staff.staffTotal = staff.staffTotal - 1" ng-disabled="staff.staffTotal === 0">-</button>
    <input class="form-control max-width" type="number" id="totalStaff" name="totalStaff" min="0"
    ng-model="staff.staffTotal" 
    ng-blur="myform.totalStaff.$touched=true" 
    ng-required="true" 
    ng-class="{'input-error': myform.totalStaff.$touched && myform.totalStaff.$error.required}"
    ng-if="isEmpty"  
    />
    <button class="form-button" ng-click="staff.staffTotal = staff.staffTotal + 1">+</button>
    </div>
</div>

controller isEmpty function

    $scope.isEmpty = function(number) {         
        return (number == undefined || number == null || number == "");
    };

So at this stage it does everything but either assign the value of 0, or the value by the user. I want the values to default to 0, as they are not all mandatory, but the form won't allow submissions with empty input fields.

1
  • Why can't you just assign the model a default value of 0? Commented Feb 23, 2017 at 1:46

2 Answers 2

1

in the initialization of scope, set staff.staffTotal to be 0. i.e.

$scope.staff.staffTotal = 0;
$scope.isEmpty = function() {
  return $scope.staff.staffTotal == 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think the Angular way would be that you initialize staff.staffTotal as 0 somewhere in your controller.

Otherwise I'd just go with adding value="0"

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.