Javascript 使用angular从Json获取数据
下面是我的代码: post.jsonJavascript 使用angular从Json获取数据,javascript,json,angularjs,Javascript,Json,Angularjs,下面是我的代码: post.json [ { "client_id":"100", "client_name":"MM Hope House", "user_fname":"Betty", "user_lname":"Johnson", "user_id":"10", "username":"bjohnson", "total_web":"$500", "cam
[
{
"client_id":"100",
"client_name":"MM Hope House",
"user_fname":"Betty",
"user_lname":"Johnson",
"user_id":"10",
"username":"bjohnson",
"total_web":"$500",
"campaigns":{
"campaign":[{
"id":"23",
"campaign_name":"MM Hope House",
"start_date":"4/15/2015",
"end_date":"6/13/2015",
"goal":"$20,000",
"total_pledges":"$1550",
"total_donations":"$1000"
}],
"pledgees":[{
"pledgee_fname":"Tavonia",
"pledgee_lname":"Evans",
"pledge_email":"tavonia@gmail.com",
"pledge_date":"4/22/2015",
"pledge_amount":"$50.00",
"paid":"$50.00",
"last_pledge":"$50.00"
}],
"donors":[{
"donor_fname":"Pat",
"donor_lname":"Smith",
"donor_email":"patsmith@onlinemediainteractive.com",
"total_cdonation":"$10.00",
"total_ldonation":"$450.00",
"last_donation":"$200.00",
"last_pledge":"$350.00"
}]
}
}
]
我的HTML代码:
<script>
function PostsCtrlAjax($scope, $http) {
$http({method: 'POST', url: 'assets/js/posts.json'})
.success(function(data) {
console.log(data);
$scope.posts = data;
})
.error(function(data, status) {
$scope.posts = data || "Request failed";
});
}
</script>
函数PostsCtrlAjax($scope,$http){
$http({method:'POST',url:'assets/js/posts.json'})
.成功(功能(数据){
控制台日志(数据);
$scope.posts=数据;
})
.错误(功能(数据、状态){
$scope.posts=data | |“请求失败”;
});
}
我要在其中填充数据的HTML代码:
<thead>
<tr>
<th>Campaign ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Amount</th>
<th>Email ID.</th>
<th>Pledge Date</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="post in posts" >
<td>{{post.id}}</td>
<td>{{post.pledgee_fname}}</td>
<td>{{post.pledgee_lname}}</td>
<td>{{post.pledge_amount}}</td>
<td>{{post.pledge_email}}</td>
<td>{{post.pledge_date}}</td>
活动ID
名字
姓
数量
电子邮件ID。
承诺日期
{{post.id}
{后质权人姓名}
{{后质权人姓名}
{{邮政质押金额}
{{post.Assign_email}
{{邮政承诺{日期}
我正在尝试从数组中获取
客户端id pleg\u fname
,但不知道如何获取 由于posts对象包含一个元素数组,因此应该使用posts[0]
获取第一个(也是唯一一个)元素。此外,您的受押人数组位于活动对象内部,因此发布[0]
将成为发布[0].compaigns.swarkeees
<tr ng-repeat="post in posts[0].campaigns.pledgees" >
<td>{{posts[0].campaigns.campaign[0].id}}</td>
<td>{{post.pledgee_fname}}</td>
<td>{{post.pledgee_lname}}</td>
<td>{{post.pledge_amount}}</td>
<td>{{post.pledge_email}}</td>
<td>{{post.pledge_date}}</td>
你应该试试这个:
<tr ng-repeat="post in posts[0].campaigns.pledgees" >
<td>{{post.id}}</td>
<td>{{post.pledgee_fname}}</td>
<td>{{post.pledgee_lname}}</td>
<td>{{post.pledge_amount}}</td>
<td>{{post.pledge_email}}</td>
<td>{{post.pledge_date}}</td>
</tr>
您可以在这里看到结果:您的
warevess
对象是一个对象数组,因此您应该改为这样做,并且您的整个JSON都包含在一个数组中,因此您需要第一个元素,它将是您的整个对象,您可以使用posts[0]调用它
然后指定要迭代的对象:抵押人
<tr ng-repeat="post in posts[0].campaigns.pledgees" >
<td>{{posts[0].campaigns.campaign[0].id}}</td>
<td>{{post.pledgee_fname}}</td>
<td>{{post.pledgee_lname}}</td>
<td>{{post.pledge_amount}}</td>
<td>{{post.pledge_email}}</td>
<td>{{post.pledge_date}}</td>
不,我正在尝试从json中填充数据…我得到的数据是正确的,但对于数组,我不知道如何获得它。标题是:“我想使用angular从json中获取数据”post.id是从活动中获得的,我如何获得此内容也可以查看我的原始帖子,已编辑。使用此解决方案,它可以获得正确的索引(对于承押人也是如此),欢迎使用!:)我编辑了我的帖子来清除我的答案你看到了吗?所有其他数组都包含这个数组。你是对的,我错过了那个数组。查看我的更新答案。但是我将如何获得活动id,而我将如何获得活动id{{post.id}
{{posts[0].campaigns.campaign[0].id}}