用$http的方法请求json数据(angularjs)附加时间间隔和时间等待
2017-10-13 19:39
417 查看
用$http的方法请求json数据,
下面一共写了俩种方法 其实都一样只是 , 一个有输入框进行查询如下:
第一个:
首先上图:
没有输入框的
<!DOCTYPE html><html lang="en" ng-app="myApp"><head><meta charset="UTF-8"><title>用$http的方法请求json数据</title><script src="angular-1.3.0.js"></script><script>var app=angular.module("myApp",[]);//json串连接app.value("url","https://free-api.heweather.com/v5/weather?city=beijing&key=545d63e185fc48169a43cbabba6e74d2")app.controller("myCtrl",function ($scope,$http,url) {//$http:服务向服务器发送请求,应用响应服务器传过来的数据。//$http.get(url)适用于读取数据的函数$http.get(url).then(function (response) {//开发者工具,主要是查看哪里报错console.log(response.data);//一层一层的找到需要的数据名称$scope.city=response.data.HeWeather5[0].basic.city;$scope.brf=response.data.HeWeather5[0].suggestion.comf.brf;$scope.txt=response.data.HeWeather5[0].suggestion.air.txt;},function () {//开发者工具,主要是查看哪里报错console.log(response.status)})});</script></head><body><div ng-controller="myCtrl">//在json串中的数据名称<div>城市:{{city}}</div><div>空气质量:{{brf}}</div><div>注意事项:{{txt}}</div></div></body></html>
第二个:
有输入框的:
上代码:
<!DOCTYPE html><html lang="en" ng-app="myApp"><head><meta charset="UTF-8"><title>用$http的方法请求json数据</title><script src="angular-1.3.0.js"></script><script>var app=angular.module("myApp",[]);app.controller("myCtrl",function ($scope,$http) {//input 页面的框里输入的值传递给下面的json串$scope.cx="";//这里设置一个button按钮进行点击运行$scope.cha=function () {$http({//指定的类型method:"GET",//json串连接 这里需要在页面传递过来输入的值不断变化所以要写成活的url:"https://free-api.heweather.com/v5/weather?city="+$scope.cx+"&key=545d63e185fc48169a43cbabba6e74d2"}).then(function (dd) {//一层一层的找到需要的数据名称$scope.city=dd.data.HeWeather5[0].basic.city;$scope.brf=dd.data.HeWeather5[0].suggestion.comf.brf;$scope.txt=dd.data.HeWeather5[0].suggestion.air.txt;})}})</script></head><body><div ng-controller="myCtrl"><input type="text" ng-model="cx"> <button ng-click="cha()">查询</button><div>城市:{{city}}</div><div>空气质量:{{brf}}</div><div>注意事项:{{txt}}</div></div></body></html>
请求本地json数据:
上图:
需要注意的是把json串文件放在代码工具的同一文件夹下:
接下来上代码:
<!DOCTYPE html><html lang="en" ng-app="myApp"><head><meta charset="UTF-8"><title>用$http的方法请求json数据 本地</title><script src="angular-1.3.0.js"></script><script>var app=angular.module("myApp",[]);app.controller("booksa",function ($scope,$http) {$http.get("book.json").then(function (response) {$scope.book=response.data;},function (response) {alert(response.status);})})</script></head><body><div ng-controller="booksa"><ul><li>ID:{{book.id}}</li><li>标题:{{book.title}}</li>4<li>类型:{{book.type}}</li><li>描述:{{book.description}}</li><li>图片:<img src="{{book.picture}}"></li><li>是否推荐:{{book.isRecommend}}</li><li>上架时间:{{book.dtCreated}}</li></ul></div></body></html>=====================================================================================
最后附加 时间间隔 和时间等待:
上图:
代码如下:
<!DOCTYPE html><html lang="en" ng-app="myApp"><head><meta charset="UTF-8"><title>显示时间的方法 </title><script type="text/javascript" src="angular-1.3.0.js"></script></head><body><div ng-controller="myCtrl"><div>{{name}}</div><div>等待5秒显示文字:{{namea}}</div></div><script>var aa = function () {//时间 年月日 时分秒return new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString();};var app=angular.module("myApp",[]);app.controller("myCtrl",function ($scope,$interval,$timeout) {//时间间隔 一秒执行一次$interval(function () {$scope.name=aa();},1000);//时间等待 等待5秒$timeout(function () {$scope.namea="你好,!"},5000);});</script></body></html>
相关文章推荐
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- AngularJS 用 $http.jsonp 方法跨域请求数据错误的问题
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- AngularJS 用 $http.jsonp 方法跨域请求数据错误的问题
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- HTTP请求数据返回解析Josn org.codehaus.jackson.map.JsonMappingException的解决方法
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- 【转载]HTTP 请求已超过分配的时间。 解决方法
- java http 请求之post json数据
- 初涉Ajax,以post或get方法发送数据,以json或xml形式接收服务器返回的请求
- android通过httpClient请求获取JSON数据并且解析
- AJAX跨域请求json数据的实现方法
- android通过httpClient请求获取JSON数据并且解析
- 使用jquery Ajax的post方法或直接使用jqery ajax请求action 得到返回的json数据
- ajax请求Struts2返回JSON数据方法
- HTTP客户端之使用request方法向其他网站请求数据
- jQuery.get方法请求JSON数据,解析JSON数据 注意事项
- AJAX跨域请求json数据的实现方法
- 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法
- 安卓通过httpget传入json参数,请求数据