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 set search.isOrdered based on a different ng-model you use for this textbox – Ian Commented Jul 10, 2014 at 19:53
Add a ment  | 

2 Answers 2

Reset to default 3

Use 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