angularjs - 从 ajax jquery 迁移到 angularjs $http
问题描述
我正在将我的项目从 ajax jquery 迁移到 angularjs。其中我的代码的目标是将 json 对象显示到 html 表中。
我已经尝试了一些示例代码来使用 angularjs 显示 json 数据/对象,但它仍然不起作用。
这是我在 ajax jquery 中的代码:
$.ajax({
type:"GET",
datatype:"json",
url:"http://localhost:7878/server/getrecords",
success: function(){
//alert("Successful!");
}
}).then(function(json){
var user = json;
console.log(user);
$.each(json, function(key, value){
var strappend = '<tr><td>' + value.billingCycle + '</td>'
+ '<td>' + value.startDate + '</td>'
+ '<td>' + value.endDate + '</td>'
+ '<td>' + value.customerFirstName + '</td>'
+ '<td>' + value.customerLastName + '</td>'
+ '<td>' + value.amount + '</td></tr>';
$("table tbody").append(strappend);
console.log(key);
});
});
});
我希望预期的输出在 html 表中显示所有报告。我希望你能帮助我。谢谢!
解决方案
如果您在控制器中执行此操作:
$http({
method: 'GET',
url: 'http://localhost:7878/server/getrecords'
}).then(function(response) {
try {
$scope.users = JSON.parse(response.data);
} catch (e) {}
});
在模板中:
...
<tr ng-repeat="user in users">
<td>{{user.billingCycle}}</td>
<td>{{user.startDate}}</td>
<td>{{user.endDate}}</td>
<td>{{user.customerFirstName}}</td>
<td>{{user.customerLastName}}</td>
<td>{{user.amount}}</td>
</tr>
...
更好的方法是使用方法创建服务,getrecords
并且在控制器中只需从服务中调用此方法
推荐阅读
- php - 如何在谷歌云存储上上传多个文件,如批量插入
- javascript - 单击事件侦听器仅适用于从 mongoDB 和 node.js 生成的第一个列表项
- .net - 在 Mathematica 中创建 PetroSIM COM 对象失败
- c++ - 在二维向量中查找非支配点
- material-ui - material-ui Snackbar 组件错误:不能作为后代出现
- r - 如何提取满足一列条件但在另一列排序的行?
- java - 用户入职状态机 - 存储和实现
- jenkins - 如何定义管道阶段一次并多次使用它
- json - 为什么我的 XML http 请求返回的是 HTML 而不是 JSON?
- ios - 构建带有或不带有今日扩展的应用程序?