我剛剛開始瞭解angular.js,所以我真的是一個新手。我不知道是否有語法問題或任何其他簡單的問題,但我無法做到這一點。
我已從Dan Wahlin's tutorial開始。我會用Rodrigo Branas的書。
<html ng-app="customersApp">
<head>
<title>App</title>
<script src="angular.min.js"></script>
<script>
var app = angular.module('customersApp', []);
app.controller('CustomersController', function($scope){
$scope.customers = [
{"id": 1, "name": "Yusuf", "age": 18},
{"id": 2, "name": "Ahmetcan", "age": 17},
{"id": 3, "name": "Ender", "age": 20},
{"id": 4, "name": "Batuhan", "age": 16},
];
$scope.add = function (name){
$scope.customers.push(angular.copy(name));
delete $scope.name;
};
});
</script>
</head>
<body>
<input type="text" id="name" ng-model="nameTxt"/> <input ng-click="add(nameTxt)" type="submit"/>
<table ng-controller="CustomersController">
<tr ng-repeat="cust in customers | filter:nameTxt">
<td>{{cust.name}}</td>
<td>{{cust.age}}</td>
</tr>
</table>
<br/>
You are looking for: {{nameTxt}}
</body>
</html>
除了$ scope.add函數,一切都很完美。我嘗試的是獲取nameTxt並將其添加到$ scope.customers。
我認爲問題出在JSON數據結構和我試圖添加的數據之間。但只是不知道如何解決這個問題。目前,感謝..
編輯:Here is the example of Rodrigo..
我真的需要那麼多的代碼?當我手動添加angularjs允許我添加沒有ID和年齡.. – 2014-10-29 09:02:26
好吧,但你不能'ng-repeat'當你添加這樣的? – 2014-10-29 09:04:26
爲什麼不呢?有什麼區別? – 2014-10-29 09:07:18