【发布时间】:2015-12-18 19:16:00
【问题描述】:
假设我有一个下拉列表,我在我的 html 模板中动态添加:
<button type="button" ng-click="addRow()" style="margin-bottom:5px;">Add cities </button>
<div ng-repeat="city in cities">
<select ng-model="cities_$index"
ng-options="n.id as n.name for n in citiesList"
class="form-control"
ng-required="true">
<option value="">-- Choose City --</option>
</select>
</div>
角度控制器:
$scope.addRow = function () {
var newrow = [];
if ($scope.cities.length === 0) {
newrow.push({ 'cities': $scope.cities_0 });
}
else {
$scope.cities.forEach(function (row, key) {
var city= '$scope.cities_'+key; // in php i can do this
//but i dont know how to do it with Js/Angular
console.log('row' + city);
newrow.push({ 'cities': city});
console.log(newrow);
});
}
$scope.cities.push(newrow);
}
我试图从所选城市中检索值,但我有未定义的值。
$scope.send = function () {
angular.forEach($scope.cities, function (value, key) {
console.log(value.id); // the id from the ng-options
};
在常规的 html 代码中,我只需添加 name="city" 并检索输入框中添加的所有城市。但是我如何从控制器中的 ng-model 中检索这些城市?
我知道我做错了什么,但由于我是 Angular 新手,我正在尝试!!
谢谢
【问题讨论】:
-
在您的情况下,保存数组的不是“数据”吗?
-
请包含您的控制器代码,这样我们就不必假设任何事情。
-
我更新了我的第一个问题并添加了控制器代码以便更好地理解我的需求