Home > Software engineering >  Angular, when toggling between two inputs clean request
Angular, when toggling between two inputs clean request

Time:03-26

I have a method that is toggling between two inputs.

toggleModToLod(configurableOption) {
      console.log(configurableOption);
      configurableOption.isModField= !configurableOption.isModField;
  }
<a ng-click="$ctrl.toggleModToLod(configurableOption)" ng-init="">MOD or LOD</a>
<input ng-if="configurableOption.isModField" type="text"  ng-model="value.mod" placeholder="MOD">
<input ng-if="!configurableOption.isModField" type="text"  ng-model="value.lod" placeholder="LOD">

It works fine, but if i had before filled the MOD field and after that i changed to LOD field the request contain both values, is there any way to have in my request only the selected value from the selected input ?

CodePudding user response:

just clear both values from model everytime you toggle:

toggleModToLod(configurableOption) {
    console.log(configurableOption);
    configurableOption.isModField= !configurableOption.isModField;
    $scope.value.mod = undefined; // however you're setting these on the controller / directive
    $scope.value.lod = undefined;
}

CodePudding user response:

You can make the switch a toggle or checkbox or other form element

<label><input type='radio' ng-model="value.which" value='MOD' /> MOD</label>
<label><input type='radio' ng-model="value.which" value='LOD' /> LOD</label>

<input ng-if="value.which=='MOD'" type="text"  ng-model="value.mod" placeholder="MOD">
<input ng-if="value.which=='LOD'" type="text"  ng-model="value.lod" placeholder="LOD">

Then when you send your request, you'll have the which variable which will tell you which value to use. Additionally, you'll have a built in switcher that will automatically work by setting value.which

  • Related