需要解決的問題:
- 本地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}})})
…