我需要从视图访问变量utc
。这是我的工厂,应该返回utc
。从视图调用工厂输出
(function() {
'use strict';
angular.module('weatherapp.weatherlist')
.controller('WeatherlistController', function(utcFactory) {
var vm = this;
vm.UTC = utcFactory.myUTC();
})
.factory('utcFactory', function() {
var myUTC = function() {
var offset = -new Date().getTimezoneOffset();
var utc = ((offset > 0 ? '+' : '') + offset/60);
return utc;
}
return {
myUTC: myUTC
}
});
})();
我已经将utcFactory
添加到控制器中,如下所示。
angular.module('weatherapp.weatherlist', ['weatherapp.locations'])
.controller('WeatherlistController', ['$scope', 'LocationService', 'WEATHER_API_URL', 'WEATHER_API_IMAGE_URL', 'WEATHER_API_ID', 'WeatherListFactory', 'utcFactory',WeatherListCtrl]);
但出于某种原因,我不能在任何地方访问utc
变种,当我尝试测试它与控制台日志中我没有看到它。
我试图使用 {{UTC}}`
什么我做错了访问它?
这里是我的html
<ion-view view-title="Whatever The Weather">
<ion-content>
<div ng-show="noLocation" class="card">
<div class="item item-text-wrap">
<div class="item">
<p>There are currently no locations available. Feel free to add new ones.</p>
</div>
</div>
</div>
<div ng-hide="noLocation" ng-repeat="weatherdata in locationData" class="card">
<div class="item item-divider icon-left weatherlist-title">
<img ng-src="{{weatherdata.weather[0].icon}}"><span>{{weatherdata.name}}, {{weatherdata.sys.country}} - {{weatherdata.weather[0].description}}</span>
</div>
<div class="item item-text-wrap">
<div class="item">
<div class="row">
<div class="col"><h2>Temperature:</h2></div>
<div class="col col-40"><b>{{weatherdata.main.temp | fahrenheit}} °F/{{weatherdata.main.temp | roundme}} °C</b></div>
</div>
<div class="row">
<div class="col"><h2>Sunset:</h2></div>
<div class="col col-30">{{weatherdata.sys.sunset | amFromUnix | amLocal | amDateFormat:'HH:mm a'}} <span class="tiny" ng-bind="'utc ' + weatherdata.utc"></span> </div>
</div>
<div class="row">
<div class="col"><h2>Wind Speed:</h2></div>
<div class="col col-30">{{weatherdata.wind.speed}} ms/{{weatherdata.wind.speed | mph}} mph</div>
</div>
<div class="row">
<div class="col"><h2>Humidity:</h2></div>
<div class="col col-30">{{weatherdata.main.humidity}} %</div>
</div>
</div>
</div>
</div>
</ion-content>
</ion-view>
谢谢,我几乎明白了,我认为ng-app标签是
'ng-repate标签是'ng-repeat =“weatherData in locationData”,我将它添加到的控制器是'angular.module('weatherapp.weatherlist') .controller('WeatherlistController',function(utcFactory)'我在这里感到困惑,范围,这些名称需要保持不变,以显示当前显示的其他信息。你能看到我在哪里出错吗? –它给出了任何错误? – developer033
不,只是没有utc在视图中。 –