需要解决的问题:
- 本地ip的获取
http://ipinfo.io/json?这个可以返回用户的ip - API的接入与使用($.ajax)
为什么在chrome的console中可以运行的js,在编辑器中无法运行?
getWeather()这个函数没有运行。Why?
改用AngularJS解决:
return $http.jsonp("http://ipinfo.io/json?callback=JSON_CALLBACK")
可以返回得到详细ip信息,
{{xx}}用作数据显示
.factory(“xx”,function(){
})自定义命令
首先是应用程序:
var app=angular.module("weather",[]);
然后是自定义命令
app.factory("weatherApi",function($http){
var obj={}
obj.getIP=function(){ //得到包括城市的详细ip地址
return $http.jsonp("http://ipinfo.io/json?callback=JSON_CALLBACK")}
obj.getCurrent=function(ip){
var api = "http://v.juhe.cn/weather/ip?format=1";
var APPKey = "&key=f0b947e4df5cffc9a5934348f626c834&ip=";
var cb = "&callback=JSON_CALLBACK";
return $http.jsonp(api + APPKey + ip + cb);
};}
return obj}
然后是控制器
app.controller("myCtrl",function($scope,Weather){
Weather.getIP().success(function(data){
$scope.Data={};
var ip=data.ip;
Weather.getCurrent(ip).success(function(data){
$scope.Data=data.result}})})
…