想着看教程 写一个类似Currently的天气预报的Chrome扩展程序 ,我想先在视图中调用这个Weather api的结果,结果就gg了。实在没辙了!
/**
* Created by lv on 2015/11/7.
*/
var app = angular.module('myapp',[]);
app.provider('Weather',function(){
var apiKey = "";
this.getUrl = function(type,ext){
return "http://api.wunderground.com/api/" + this.apiKey + "/" + type + "/q/" + ext + '.json';
};
this.$get = function($q,$http){
var self = this;
return {
//服务对象
getWeatherForecast: function(city){
var d = $q.defer();
$http({
method: 'GET',
url: self.getUrl("forecast",city),
cache:true
}).success(function(data){
//Wunderweather接口返回
//嵌套在forecast.simpleforcast属性内的forecast对象
d.resolve(data.forcast.simpleforcast);
}).error(function(err){
d.reject(err);
});
return d.promise();
}
}
};
this.setApiKey = function(key){
if(key) this.apiKey = key;
};
});
app.config(function(WeatherProvider){
WeatherProvider.setApiKey('39fb2e6f07af19f7-Yzz');
});
app.controller('mainController',function($scope,$timeout,Weather){
//构建date对象
$scope.date = {};
//时钟
var upDateTime = function(){
$scope.date.raw = new Date();
$timeout(upDateTime,1000);
};
//启动函数
upDateTime();
$scope.weather = {};
Weather.getWeatherForecast("CA/San_Francisco")
.then(function(data){
$scope.weather.forcast = data;
});
});
<p ng-controller="mainController">
<p id="forecast">
<pre>{{ weather.forecast | json }}</pre>
</p>
</p>
第一个错误,有位哥们帮我解决了,第二个Cannot read property 'simpleforcast' of undefined 该怎么办-。-!!! 我这样写能获取到api吗?