Angular项目

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script type="text/javascript" src="js/angular.js" ></script>
		<script type="text/javascript" src="js/angular-route.js" ></script>
		<title></title>
		<style>
			table tr:nth-child(even){
				background: burlywood;
			}
			table tr:nth-child(odd){
				background: cadetblue;
			}
		</style>
		<script>
			var app=angular.module("myApp",[]);
			app.controller("myCtrl",function($scope,$http){
				
				$scope.jsonData="";
				//网络请求数据
				$http({
					method:"get",
					url:"http://gank.io/api/data/Android/10/1"
				}).then(function success(response){
					$scope.jsonData=response.data;//获取到网络请求的数据
				},function error(response){
					
				});
				
				
				//排序
				$scope.orderKey="";
				//过滤
				$scope.search="";
				//删除
				$scope.showtable=true;
				
				$scope.del=function($index){
					$scope.jsonData.results.splice($index,1);
				if($scope.jsonData.results.length==0){
					//隐藏表
					$scope.showtable=false;
				}
				}
				
				$scope.ifdiv=false;
				$scope.addnews=function(){
					//点击添加新闻  显示添加框
					$scope.ifdiv=!$scope.ifdiv;
						$scope.ifdiv2=false;
				}
				//获取要添加的值
				$scope.newtype="";
				$scope.newsource="";
				$scope.newwho="";
				$scope.newcreatedAt="";
				
				//添加数据
				$scope.addinfo=function(){
					//将要添加的数据封装成一个对象
					var newsdata={
						type:$scope.newtype,
						source:$scope.newsource,
						createdAt:new Date(),
						who:$scope.newwho
						
					}
					//将添加的数据赋值给表的属性
					$scope.jsonData.results.push(newsdata);
					//点击添加  添加框消失
					$scope.ifdiv=false;
					$scope.showtable=true;
				
				}
				
				//点击修改  弹出修改框
				$scope.ifdiv2=false;
				$scope.myupdate=function($index){
					$scope.ifdiv2=true;
					$scope.i=$index;
					
					$scope.newtype=$scope.jsonData.results[$index].type;
					$scope.newsource=$scope.jsonData.results[$index].source;
					$scope.newcreatedAt=$scope.jsonData.results[$index].createdAt;
					$scope.newwho=$scope.jsonData.results[$index].who;
					$scope.ifdiv=false;
				}
				//修改框中的修改
				$scope.updateinfo=function(){
					$scope.jsonData.results[$scope.i].type=$scope.newtype;
					$scope.jsonData.results[$scope.i].source=$scope.newsource;
					$scope.jsonData.results[$scope.i].createdAt=$scope.newcreatedAt;
					$scope.jsonData.results[$scope.i].who=$scope.newwho;
			        $scope.ifdiv2=false;
				}
				
			});
			
		</script>
	</head>
	<body ng-app="myApp" ng-controller="myCtrl">
	<center>
		<h3 style="width: 160px; height: 25px; background: green;">IT权威新闻</h3>
		<input type="text" ng-model="search" placeholder="请输入查询关键字"/>
		<select ng-model="orderKey">
			<option value="">--请选择--</option>
			<option value="createdAt">时间升序</option>
			<option value="-createdAt">时间降序</option>
			<option value="type">type升序</option>
			<option value="-type">type降序</option>
		</select>
		<input type="button" value="添加新闻" ng-click="addnews();"/>
		<br /><br />
		<table  ng-show="showtable" border="1" cellpadding="0" cellspacing="0">
			<tr style="background: darkgrey;">
				<th>序号</th>
				<th ng-click="orderKey='type'">
					type
				</th>
				<th>source</th>
				<th>createdAt</th>
				<th >who</th>
				<th>操作</th>
			</tr>
			<tr ng-repeat="d in jsonData.results | orderBy:orderKey | filter:search">
				<td>{{$index}}</td>
				<td>{{d.type}}</td>
				<td>{{d.source}}</td>
				<td>{{d.createdAt}}</td>
				<td>{{d.who}}</td>
				<td>
					<input type="button" value="删除" ng-click="del($index);"/>
					<input type="button" value="修改" ng-click="myupdate($index);"/>
				
				</td>
				
			</tr>
		</table><br /><br />
		<div ng-show="ifdiv">
			type:<input type="text" ng-model="newtype"/><br />
			source:<input type="text" ng-model="newsource" /><br />
			createdAt:<input type="text" ng-model="newcreatedAt"/><br />
			who:<input type="text" ng-model="newwho"/><br />
		  <input type="button" value="添加" ng-click="addinfo();"/>
		</div>
		<div ng-show="ifdiv2">
			type:<input type="text" ng-model="newtype"/><br />
			source:<input type="text" ng-model="newsource" /><br />
			createdAt:<input type="text" ng-model="newcreatedAt"/><br />
			who:<input type="text" ng-model="newwho"/><br />
		  <input type="button" value="修改" ng-click="updateinfo();"/>
		</div>
		
	</center>
	</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值