控制器未定义 ng-model
ng-model is undefined from controller
嗨,
为什么 foo()
函数的值未定义?
我发现问题是如果模型名称只是 brand
但是,这不是我的问题。我的模型名称是 car.brand
.
所以我的问题是 - 如何从 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);
}
谢谢。
因为你用的是ng-repeat
,你会有很多车,所以你的模型填在车里:
$scope.foo = function() {
for(var i in $scope.cars) {
alert("foo: " + $scope.cars[i].brand);
}
}
您需要使用 car.brand as
或 car.model as
<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.brand 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.model as car.model for car in cars[$index].models">
<option value="">select</option>
</select>
</div>
<button ng-click="foo()">
save all
</button>
</div>
或者
<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" 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" ng-options="car.brand as car.model for car in cars[$index].models">
<option value="">select</option>
</select>
</div>
<button ng-click="foo()">
save all
</button>
</div>
as
之前的任何内容都是 ng-model
将获得的值
嗨,
为什么 foo()
函数的值未定义?
我发现问题是如果模型名称只是 brand
但是,这不是我的问题。我的模型名称是 car.brand
.
所以我的问题是 - 如何从 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);
}
谢谢。
因为你用的是ng-repeat
,你会有很多车,所以你的模型填在车里:
$scope.foo = function() {
for(var i in $scope.cars) {
alert("foo: " + $scope.cars[i].brand);
}
}
您需要使用 car.brand as
或 car.model as
<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.brand 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.model as car.model for car in cars[$index].models">
<option value="">select</option>
</select>
</div>
<button ng-click="foo()">
save all
</button>
</div>
或者
<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" 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" ng-options="car.brand as car.model for car in cars[$index].models">
<option value="">select</option>
</select>
</div>
<button ng-click="foo()">
save all
</button>
</div>
as
之前的任何内容都是 ng-model
将获得的值