单击时将数据添加到行中

时间:2016-07-15 10:53:09

标签: angularjs

我希望当我将一些数据插入文本框并单击按钮时,数据应该插入到特定的表列中,当我继续添加数据时,行应该随着输入的数据而不断增加。代码:

var parking = angular.module("parking", []);
parking.controller("parkingCtrl", function($scope){
$scope.carnamebind='';
$scope.car=[];
$scope.bindcarName = function(){
var ee=$scope.car;

ee.push($scope.carname);
$scope.carnamebind=ee;
}
})

html:

<body ng-controller="parkingCtrl">
<h3 ng-model="appTitle"></h3>


<table>
<tr>
<td>Car name</td>
<td>Car model</td>
</tr>

<tr>
<td>{{carnamebind}}</td>
<td></td>
</tr>

<tr>
<td ><input type="text" ng-model="carname"/><input type="button" ng-click="bindcarName()"/></td>
<td></td>
</tr>

</table>


</body>

即将出现2个问题:

1)所有推入数据的数据都插入到同一列

2)数据以数组的形式插入,如["sd","sdasd"]

由于

1 个答案:

答案 0 :(得分:0)

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.carnamebind= [
    "Alfreds Futterkiste",
    "Berglunds snabbköp",
    "Centro comercial Moctezuma",
    "Ernst Handel",
  ];
$scope.bindcarName = function(ee){

$scope.carnamebind.push(ee);


}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html>

<body ng-app="myApp">

<table ng-controller="myCtrl" border="1">
<tr>
<td>Car name</td>
<td>Car model</td>
</tr>
<tr ng-repeat="x in carnamebind">

  <td>{{x}}</td>
  <td></td>
</tr>
<tr>
<td ><input type="text" ng-model="carname"/></td>
<td><input type="submit" ng-click="bindcarName(carname)"/></td>
</tr>
</table>



</body>
</html>