Matarishvan
Matarishvan

Reputation: 2422

ng-model select dropdown update - angular

Basically i have a select dropdown

<select class="form-control" id="ursel"
        ng-change="changeVal()"
        ng-options="a for a in RatedVoltage" data-ng-model="TechCharacters.selectedUr">
        <option value="" selected="selected">{{globalLangResource.SelectAny}}</option>
</select>

So on change of this select, have results reflecting to another select

viewModel.changeVal = function(){
    var val = viewModel.TechCharacters.selectedUr; 
    if (val != undefined && val === "7.2kV") {
        $rootScope.ud = viewModel.InsulationVoltage["7.2kV"];  // 20,30,40  
    }
}

The 2nd dropdown looks like this

<select class="form-control" id="udsel"
        ng-change="setUd();"
        ng-options="a for a in ud" data-ng-model="TechCharacters.InsulVolt">
       <option value="" ng-if="false"></option>
</select>

Now i have a submit button, on submit,i am getting the main object.

console.log(TechCharacters);

Here i am not getting TechCharacters.InsulVolt value. it is showing empty.

If i have made change is the 2nd dropdown, the model is updated. until then i am not getting the changed model from 1st dropdown

Basically i want all the ng-model values inside form even it is changed or not.

Upvotes: 1

Views: 677

Answers (2)

Arun
Arun

Reputation: 684

You can actually do this. This will work.

<select data-ng-model="TechCharacters.selectedUr"
    <option ng-selected="{{obj == TechCharacters.selectedUr}}" value="{{obj}}" ng-repeat="(key,value) in RatedVoltage">
</select>

Basically you are doing ng-selected and comparing the ng-model value with that of the ng repeat value, and the selected value will be shown.

Upvotes: 0

Viplock
Viplock

Reputation: 3319

if you want the first object of second dropdown on change of first one. change code to .

viewModel.changeVal = function(){
    var val = viewModel.TechCharacters.selectedUr; 
    if (val != undefined && val === "7.2kV") {
        $rootScope.ud = viewModel.InsulationVoltage["7.2kV"];  // 20,30,40  
        viewModel.TechCharacters.InsulVolt=$rootScope.ud[0]
    }
}

hope it will help you

Upvotes: 1

Related Questions