Hi,
Why the value from foo()
function is undefined?
I found that the problem is if the model name is only brand
but, it is not my problem. The name of my model is car.brand
.
So my question is - how to access model from ctrl?
HTML:
<div ng-controller="MyCtrl">
<div ng-if="cars" ng-repeat="car in cars">
<br>
<label>{{car.name}}</label>
<br>
<label>brand</label>
<select ng-model="car.brand" ng-options="car as car.name for car in brands" ng-change="loadBrands($index)">
<option value="">select</option>
</select>
<label>model</label>
<select ng-model="brand.model" ng-options="car as car.model for car in cars[$index].models">
<option value="">select</option>
</select>
</div>
<button ng-click="foo()">
save all
</button>
</div>
JS:
$scope.foo = function() {
alert("foo: " + $scope.car.brand);
}
Thanks.
As you are using ng-repeat
, you will have many cars, so your model you be filled in the cars:
$scope.foo = function() {
for(var i in $scope.cars) {
alert("foo: " + $scope.cars[i].brand);
}
}