Reputation: 821
I have following HTML code:
<input type="text" ng-model="search.isOrdered"/>
If user types ordered
word to input then I want ng-model = true
, if user types unordered
word to input, then I want ng-model = false
. In other cases I want to ng-model = undefined
or ng-model = null
. Suppose I can't use radio-button or checkbox.
How to achieve this?
Thanks
Upvotes: 2
Views: 7894
Reputation: 2599
Change your HTML as follows:
<input type="text" ng-model="search.isOrderedInput"/>
In your controller:
$scope.isOrdered = function() {
var isOrdered;
switch ($scope.isOrderedInput) {
case 'ordered':
isOrdered = true;
break;
case 'unordered':
isOrdered = false;
break;
default:
isOrdered = undefined;
break;
}
return isOrdered;
};
This should be more maintainable than adding logic to the view via ng-change, no?
Upvotes: 2
Reputation: 7346
Use ng-change
instead:
<input type="text" ng-model="search.order" ng-change="search.isOrdered = search.order == 'ordered'"/>
See working fiddle
Upvotes: 3