我刚刚开始了解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