Here is my html: I have an object with arrays of items. I need to add a new array with values from inputs.
这是我的html:我有一个带有项目数组的对象。我需要添加一个包含输入值的新数组。
<div ng-repeat="item in contact.items">
<label for="Title">Title</label>
<select id="Title" ng-model="item.title">
<option ng-selected="title.value === item.title" ng-repeat="title in titles">
{{ title.title }}
</option>
</select>
</div>
<div class="row">
<label for="Name"> Name</label>
<input type="text" id="Name" placeholder="Name" ng-model="item.name">
</div>
<div class="row">
<a ng-click="addItem();">Save Item</a>
</div>
Here is the controller:
这是控制器:
$scope.addItem = function() {
$scope.contact.items.push({
name: "",
title:""
});
console.log( $scope.contact.items);
};
It is ok when I push the empty array, but it fails when I try pushing the values form the ng-model:
按空数组是可以的,但按ng-model中的值则不行:
$scope.addItem = function() {
$scope.contact.items.push({
name: $scope.item.name,
title:$scope.item.title
});
console.log( $scope.contact.items);
};
What am I missing?
我缺少什么?
1 个解决方案
#1
3
You need to store the values in an object and then push it to array.
您需要将值存储在对象中,然后将其推入数组。
$scope.item = {};
$scope.addItem = function() {
var newItem = {};
newItem.name = $scope.item.name;
newItem.title = $scope.item.title;
$scope.contact.items.push(newItem);
console.log($scope.contact.items);
};
#1
3
You need to store the values in an object and then push it to array.
您需要将值存储在对象中,然后将其推入数组。
$scope.item = {};
$scope.addItem = function() {
var newItem = {};
newItem.name = $scope.item.name;
newItem.title = $scope.item.title;
$scope.contact.items.push(newItem);
console.log($scope.contact.items);
};