可以't向我的简单JSON($scope.customers)中添加新数据
can't add a new data to my simple JSON ($scope.customers)
我刚开始学习angular.js,所以我真的是个新手。我不知道是否存在语法问题或任何其他简单的问题,但我就是无法做到这一点。
我从Dan Wahlin的教程开始。我会用罗德里戈·布拉斯的书。
<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数据结构和我试图添加的数据之间。但就是不知道该怎么解决。已经,谢谢。。
编辑:这是罗德里戈的例子。。
您的输入超出了控制器的范围,您还必须推送一个客户列表中相同类型的对象。您必须相应地计算项目的Id。
我在JsFiddle 上做了一个简单的例子
Html
<div ng-controller="CustomersController">
<input type="text" id="name" ng-model="nameTxt" />
<input ng-click="add(nameTxt)" type="submit" />
<table >
<tr ng-repeat="cust in customers | filter:nameTxt">
<td>{{cust.name}}</td>
<td>{{cust.age}}</td>
</tr>
</table>
<br/>You are looking for: {{nameTxt}}</div>
代码
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({"id": 5, "name": name, "age": 99});
};
});
使用以下代码:
$scope.add = function (name){
$scope.customers.push({'id':"",'name':name,'age':""});
};
<div ng-app="customersApp">
<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}}
</div>
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({"id":'',"name":name,"age":''});
};
});
按照以下步骤操作:
<input type="text" id="name" ng-model="nameTxt"/> <input ng-click="add()" type="submit"/>
$scope.add = function (){
$scope.customers.push({"id":$scope.customers.length,"name":nameTxt,"age":"---fill---"});
};
问题的原因是我在哪里使用了"ng controller"参数。我已经在标签中定义了它,但输入不在表中,因为这是必须的
我改了3行,问题解决了。
<body>
至
<body ng-controller="CustomersController">
因此,从表中删除了ng控制器参数,并编辑了$scope.add函数,如下所示。
$scope.add = function (name){
$scope.customers.push({"name": name});
};
我不知道为什么angular复制不起作用。
相关文章:
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在映射数组中添加换行符
- 正在将数据主题添加到所有项目
- ZeroClipboard-在复制之前添加到值
- 我可以在json对象中添加一个函数吗
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何添加浮动和非浮动,其他
- 添加文字和评论功能更新Div
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 在函数中添加数组元素的数值
- 如何将歌曲添加到jPlayer
- 在html Select中添加搜索
- Java脚本时间添加
- FabricJs-限制主对象内添加对象的移动区域
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 如何在Google柱状图中动态添加行/列
- 正在尝试使用if和else添加类,但无法正常工作
- 可以't向我的简单JSON($scope.customers)中添加新数据