【发布时间】:2016-09-20 13:11:06
【问题描述】:
我是 Angular 的新手,也是 stackoverflow 的新手,这是我的第一个问题。
所以我正在构建一个角度表,到目前为止,上帝,我已经完成了添加数据和删除数据,棘手的部分是编辑表中的数据。
我创建了两个输入文本并将它们与 ng-model 和表中的数据绑定,
但是当我单击编辑按钮时,它会从表中抓取所有数据,但我只想编辑我单击的那个编辑,
我猜我应该隐藏其他行,或者我应该让 ng-model 不抓取所有数据。我尝试了一些他们没有用的东西。我尝试了 google 和 stackoverfllow 我找到了一些示例,我应用了该代码并在我的应用程序中进行了编辑,但它没有用。
谢谢。
<<!DOCTYPE html>
<html ng-app="incercari">
<head>
<title>incercari</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body ng-controller="firstCtrl">
<input type="text" ng-model="search" placeholder="search...">
<table class="table table-bordered">
<thead>
<tr>
<th>City</th>
<th>Sport</th>
<th>Remove<th>
</tr>
</thead>
<tbody>
<tr ng-repeat="(productIndex, ppl) in cities | filter:search">
<td>{{ppl.name}}</td>
<td>{{ppl.sport}}</td>
<td><input type="button" class="btn btn-danger" value="Delete" ng-click="removeRow(productIndex)"/></td>
<td><a href="#" ng-click="edit(ppl)">Edit</a>
<input type="text" id="name" ng-model="current.name" value="{{current.name}}">
<input type="text" id="sport" ng-model="current.sport" value="{{current.sport}}">
<button ng-click="save(current)">Save</button></td>
</tr>
</tbody>
</table>
<form ng-submit="addData(cities)">
Add data<br />
<input type="text" placeholder="City" ng-model="cities.name" required >
<input type="text" placeholder="Sport" ng-model="cities.sport" >
<input type="submit" value="Submit" class="btn btn-primary">
<script src="app.js"></script>
</body>
</html>>
这是应用程序 var app = 角度 .module("incercari", []) .controller("firstCtrl", function($scope){
$scope.cities = [
{'name': 'New York', 'sport': 'basketball'},
{'name': 'Bucharest', 'sport': 'soccer'},
{'name': 'Sydney', 'sport': 'rugby'},
{'name': 'Toronto', 'sport': 'hockey'}
];
$scope.addData = function(cities) {
$scope.cities.push({'name': cities.name,
'sport': cities.sport,
});
$scope.cities.name = "";
$scope.cities.sport= "";
};
$scope.removeRow = function (productIndex) {
$scope.cities.splice(productIndex, 1);
};
$scope.edit = function(ppl) {
$scope.current = ppl;
};
$scope.current = {};
$scope.save = function(ppl) {
$scope.current = {};
};
$scope.selectedIndex = '';
});
这是我的应用的链接: http://plnkr.co/edit/vsB5lRB15RiQn6wH2kHp?p=preview
点击编辑看看会发生什么。
【问题讨论】:
标签: javascript jquery html angularjs