AngularJS重置数组的数组
AngularJS resetting array of arrays
我问了一个类似的问题,并尝试了以前没有运气的解决方案。很简单的问题。我有以下内容:
<script type="text/javascript">
'use strict';
var app = angular.module('app', ['appControllers']);
"use strict";
var appControllers = angular.module('app', []);
appControllers.controller('personController', ['$scope', '$http', function ($scope, $http) {
$scope.responses = [[], [], []];
$scope.addCoach = function() {
$scope.responses[0].push($scope.coach.name);
};
$scope.addAthlete = function() {
$scope.responses[1].push($scope.athlete.name);
};
$scope.addSupportStaff = function() {
$scope.responses[2].push($scope.employee.name);
};
所以基本上我试图将教练,运动员和支持人员的列表添加到数组内的各自数组中,但是当我检查$scope的控制台日志时。回复:只显示最近添加的回复。
编辑:所以我把我的代码改成如下:
<script type="text/javascript">
'use strict';
var app = angular.module('app', ['appControllers']);
"use strict";
var appControllers = angular.module('app', []);
appControllers.controller('personController', ['$scope', '$http', function ($scope, $http) {
$scope.responses = {
coaches: [],
athletes: [],
employees: []
};
$scope.addCoach = function() {
$scope.responses.coaches.push($scope.coach.name);
console.log($scope.responses);
$scope.coach = {}; // Do this to clean up the form fields
};
$scope.addAthlete = function() {
$scope.responses.athletes.push($scope.athlete.name);
console.log($scope.responses);
$scope.athlete = {}; // Do this to clean up the form fields
};
$scope.addSupportStaff = function() {
$scope.responses.employees.push($scope.employee.name);
console.log($scope.responses);
$scope.employee = {}; // Do this to clean up the form fields
};
}]);
HTML<h1>Coaches Attending</h1>
<div data-ng-app="app" data-ng-controller="personController">
<div>
<div>
<input type="text" class="span3" placeholder="Full Name" ng-model="coach.name">
<button type="button" ng-click="addCoach()"> Add </button> </div>
<div >
<ul><li ng-repeat="coach in responses.coaches">{{coach}}</li></ul>
</div>
</div>
<br>
<h1>Athletes Attending</h1>
<div data-ng-app="app" data-ng-controller="personController">
<div>
<div>
<input type="text" class="span3" placeholder="Full Name" ng-model="athlete.name">
<button type="button" ng-click="addAthlete()"> Add </button> </div>
<div >
<ul><li ng-repeat="athlete in responses.athletes">{{athlete}}</li></ul>
</div>
</div>
<br>
<h1>Support Staff Attending</h1>
<div data-ng-app="app" data-ng-controller="personController">
<div>
<div>
<input type="text" class="span3" placeholder="Full Name" ng-model="employee.name">
<button type="button" ng-click="addSupportStaff()"> Add </button> </div>
<div >
<ul><li ng-repeat="employee in responses.employees">{{employee}}</li></ul>
</div>
</div>
但是我现在的问题是,当我在添加任何运动员或支持人员后在教练列中添加某人时,它会删除前面的条目。
修复。我多次定义控制器和应用程序给它多个实例
为什么不使用对象呢?
$scope.responses = {
coaches: [],
athletes: [],
employees: []
};
然后像这样:
$scope.addCoach = function (contact) {
$scope.responses.coaches.push(contact.name);
};
var exampleCoach = {name: "Tim Burns", email: "test@tester.com"};
$scope.addCoach(exampleCoach);
console.log($scope.responses);
如果这没有帮助,设置一个柱塞与您的整个相关代码,包括视图。可能是你显示结果的方式有问题。
[Edit:]你的问题是数据被清除,这是因为你正在推动一个引用。所以当你更新原始的时候,引用也被清除了。当你push对象的时候,你需要做一个对象的拷贝。
试试这样写:
$scope.addCoach = function (contact) {
var clonedContact= angular.copy(contact);
$scope.responses.coaches.push(clonedContact);
};
相关文章:
- 如何将angularjs中的javascript字符串输出为循环数组
- angularjs$watch获取已更改数组项的索引
- AngularJS-ng使用字符串索引在数组上重复
- 如何使用angularJS从json的对象数组中显示和计数实体
- angularjs ng基于点击时的数组值重复过滤器
- 使用angularjs数组中的slice
- 从空格分隔的文本创建和实现数组:AngularJS
- json数组angularjs中的自定义过滤器
- 在方法中看不到对象数组.(Angularjs)
- 查找对象数组(Angularjs)中是否存在值
- $scope绑定到空值数组 AngularJs
- 同一数组(AngularJS)中的javascript对象之间的一对一关系
- 编译和传递数组 - AngularJS和PHP
- 如何根据数组AngularJS中的元素更改href
- 通过id过滤数组angularjs的数组
- 查找数组angularjs对象的索引
- 在数组angularjs javascript中选中或取消选中复选框
- 将JSON响应存储在数组angularjs中
- 如何将元素推送到指令中的数组 - AngularJS
- 动态追加值到数组angularjs