2016-07-15 51 views
0

我希望當我插入一些數據到一個文本框,並點擊一個按鈕時,數據應該插入到一個特定的表列,當我不斷增加數據的行應該繼續增加與輸入的數據。代碼:添加數據到點擊行

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"]

謝謝

回答

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>