angularjs操作json

这个示例展示了如何在AngularJS中使用$http服务获取JSON数据,并通过过滤器(filter)根据用户输入过滤显示数据。博客内容包括定义控制器、获取服务器数据、使用ng-keydown事件触发过滤函数以及在表格中展示数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>


<head>
<meta charset="UTF-8">
<title></title>
<script src="lib/angular.min.js"></script>
<script>
var app = angular.module("gaoynApp", []);
app.controller("democ", ["$scope", "$http","$filter", function($scope, $http,$filter) {
//直接从服务器获取的数据
$scope.resdata; //保存最初始数据
$scope.datas;   //视图上展示的数据

//定义过滤数组
$scope.selectbyname=function(){
console.log("----------------");
//添加过滤器 $filter(服务)--->内置9个过滤器小对象
var f=$filter("filter");//从服务中选取一个过滤"filter"---->筛选数组元素   html视图datas|fileter:
//$scope.resdata---->一开始从服务端
//$scope.datas=f($scope.resdata,$scope.gname);//$scope.gname--->从输入框查询条件

$scope.datas=f($scope.resdata,$scope.gname);
}

$scope.getData = function() {
console.log(">>>>>>>>>>>>");
//通过http请求服务端一个json数据串
//• $http.get(url)适用于读取数据的函数
//stars.json url
$http.get("stars.json").then(function(resp) {
//获取真正的数据源 本质就是数组
$scope.datas = resp.data;
$scope.resdata=resp.data;
});
}


}]);
</script>
<style>
input {
display: block;
margin: 2px;
}
</style>
</head>


<body ng-app="gaoynApp" ng-controller="democ">
<input type="button" value="获取数据" ng-click="getData()" />
<!--输入框输入内容以后,重新改变table显示数组 -->
<input ng-keydown="selectbyname()" ng-model="gname" />

<table border="1px">
<tr>
<th>姓名</th>
<th>头像</th>
<th>年龄</th>
<th>性别</th>
<th>专辑</th>
</tr>
<tr ng-repeat="data in datas">
<td>{{data.name}}</td>
<td>
<img width="100px" src={{data.photo}} alt="图片无法加载显示" />
</td>
<td>{{data.age}}</td>
<td>{{data.sex}}</td>
<td>{{data.ablum}}</td>
</tr>
</table>
</body>


</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值