beckah beckah - 1 year ago 60
AngularJS Question

Get currently selected object as an argument for another field in Angular

I have a select box that outputs all car objects in cars-array as a select option

<select name="car">
<option ng-repeat="car in cars" ng-value="car">

I want to then output all options available with a checkbox, and "check" the option if it's available for that car

<table id="options">
<tr ng-repeat="option in options">
<input type="checkbox" ng-checked="checkCarOption(car,option)"></input>

How do I make sure that the
sent into the
is the car that's currently selected?

Answer Source

You just need to set ng-model on the select:

<select name="car" ng-model="selectedCar">

And then you can do


and in the function compare it to $scope.selectedCar

In fact, you should not be doing ng-repeat on option elements. you should be using ng-options on the select element. You will run into really weird bugs later on if you don't.

<select ng-options="car as for car in cars" ng-model="selectedCar" name="car"></select>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download