admin管理员组文章数量:1387295
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
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
Share Improve this question asked Jul 10, 2014 at 19:49 WeleToWeleTo 8218 silver badges12 bronze badges 1-
I think you'll have to use
ng-change
and setsearch.isOrdered
based on a differentng-model
you use for this textbox – Ian Commented Jul 10, 2014 at 19:53
2 Answers
Reset to default 3Use ng-change
instead:
<input type="text" ng-model="search.order" ng-change="search.isOrdered = search.order == 'ordered'"/>
See working fiddle
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?
本文标签: javascriptConditionally change value of ngmodel for inputStack Overflow
版权声明:本文标题:javascript - Conditionally change value of ng-model for input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744568350a2613186.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论