【发布时间】:2015-07-19 05:56:03
【问题描述】:
我正在开发一个 web 应用程序,您可以在其中添加任意数量的公式来指定属性。对于每个公式,一个对象被推入一个具有未定义属性的数组中。当您按“全部接受”时,我想遍历所有公式并设置输入的名称和属性。如何使用输入内容设置数组中对象的属性? $scope.eventName 不起作用。可能对范围有误解。
我之前在eventCtrl 中做了同样的事情,在为每个公式单击“接受”时将属性作为新对象添加到数组中。但我想将其自动化,这样您就不必在每个公式上单击“接受”。
<body class="container" ng-controller="eventListCtrl">
<div class="well well-lg" ng-controller="eventCtrl" ng-repeat="event in events">
<div>
<label>Name:</label><input ng-model="name" ></input><br>
<label>Properties:</label><input ng-model="propertyName"></input>
<select ng-model="propertyType">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button ng-click="addProperty()">Add</button>
<div>
<select size="5" id="propertyBox" >
<option ng-repeat="property in properties"> {{property.name}} : {{property.type}}</option>
</select>
</div>
</div>
<button ng-click="deleteEvent()">Delete</button>
</div>
<button ng-click="addEvent()">add</button>
<button ng-click="acceptEvents()"> accept all</button>
</body>
还有我的 JS:
var events = [];
app.controller('eventListCtrl', function($scope) {
$scope.events = events;
$scope.addEvent = function() {
events.push({eventName: undefined, eventProperties: undefined});
console.log(events);
};
$scope.acceptEvents = function(){
angular.forEach($scope.events, function(event) {
name = $scope.eventName ;
});
console.log(events);
};
});
app.controller('eventCtrl', function($scope) {
$scope.properties = [];
$scope.addProperty = function(){
$scope.properties.push({name: $scope.propertyName, type: $scope.propertyType});
$scope.propertyName = "";
console.log(name);
};
});
【问题讨论】:
标签: javascript html arrays angularjs