AngularJS Choose another option and get the value from the text box

Using AngularJS I have a list of the selected option and one additional parameter, "New." What I'm trying to do when the "New" option is selected, a text box will be shown to add a new value.

<select ng-model="group.name" ng-options="bl.Name for bl in Groups" >
      <option value="VALUE FROM TEXT BOX">New</option>
</select> 

<div class="col-md-4" ng-show="WHEN NEW OPTION IS Chosen ">
<input class="text-box" type="text" name="NewValue" ng-model="group.name" />
</div>
+4
source share
3 answers

You can try like this

<div ng-app='myApp' ng-controller="ArrayController">
    <select ng-model="group.name" ng-options="Group.Name for Group in Groups"></select>
    <div class="col-md-4" ng-show="group.name.Value == 'new'">
        <input class="text-box" type="text" name="NewValue" ng-model="newValue" />
        <button ng-click="add(newValue)">Add</button>
    </div>
</div>
+4
source

Just watch the change in the selected item:

$scope.displayDiv = false;
$scope.newval = 'newval';
$scope.$watch('group.name', function(newval, oldval) {
    if (newval == $scope.newval) {
         $scope.displayDiv = true;
    }

}

and for HTML:

<div class="col-md-4" ng-show="displayDiv">
0
source

. .

<select ng-model="curopt" 
        ng-options="option.name for option in options">
</select>
<input type="text" ng-model="newopt" 
       ng-show="curopt.name == 'New'" 
       ng-keypress="newOption($event)">

ng-show . ng-keypress, .

...

$scope.newOption = function(event) {
  if (event.which == 13) {
    $scope.options.push({ name : $scope.newopt, value : $scope.newopt });
    $scope.newopt = '';
  }
}

Since our input selection options are bound to $scope.options, we can simply add the value of our text input to the array, and angular will handle the update for us due to data binding.

0
source

Source: https://habr.com/ru/post/1548642/


All Articles