wavern
wavern

Reputation: 69

Angularjs - Dropdown - check if no value selected

I'm attempting to verify if an option other then the default display has been selected. I can not seem to access/verify the empty value for the default case.

I never get to the inner if, I have confirmed I get past the outer. So I'm sure the issue is with my method of accessing 'program_id' I'm just hitting a wall. Any help would be greatly appreciated. Thanks.

programIdsDropDown is JSON populated from a DB.

html

<select
    name="program_id"
    class="form-control"
    ng-model="formData.program_id"
    ng-options="key as value for (key, value) in programIdsDropDown"
>
<option value="">Please select..</option>
</select>
<div ng-show="mainForm.$submitted">
<span class="text-danger" ng-show="shouldChangeProgram()">Please select a Program.<br /></span>
</div>

Controller

$scope.shouldChangeProgram = function() {
var result = false;

if($scope.formData.status == 'closed')
{
    if($scope.formData.program_id == '')
    {
        result = true;
    }
}

$scope.mainForm.status.$setValidity('shouldChangeProgram', !result);

return result;
};

Upvotes: 1

Views: 5313

Answers (1)

korteee
korteee

Reputation: 2692

You should better use :

if(!$scope.formData.program_id) {
   result = true;
}

If problem insists you should check the value of $scope.formData.program_id

Upvotes: 4

Related Questions