AngularJS范围功能不起作用,没有控制台错误

时间:2016-08-27 06:13:45

标签: angularjs

我有这个应用程序可以获得有关特定地区天气的一些数据:

enter image description here

我想通过单击C / F按钮将当前温度从摄氏温度更改为华氏温度。我尝试了以下脚本:

在app.JS中和当前页面的控制器内:

$scope.convertToFahrenheit = function(degF)
{
    return Math.round(1.8*(degF - 273) +32);
}

现在在html页面中,我做了以下内容:

Degree: 
<button ng-click="convertToCelsius(w.temp.day)" class="btn btn-info">
  C
</button> | 
<button ng-click="convertToFahrenheit(w.temp.day)" class="btn btn-info">
  F
</button>

这是所有app.JS脚本:

// MODULE
var weatherApp = angular.module('weatherApp', ['ngRoute', 'ngResource']);

// ROUTES
weatherApp.config(function ($routeProvider) {

    $routeProvider

    .when('/', {
        templateUrl: 'pages/home.htm',
        controller: 'homeController'
    })

    .when('/forecast', {
        templateUrl: 'pages/forecast.htm',
        controller: 'forecastController'
    })

    .when('/forecast/:days', {
        templateUrl: 'pages/forecast.htm',
        controller: 'forecastController'
    })

});

//Services

weatherApp.service('cityService', function()
{
    this.city = 'Rayak, ZA';
});
// CONTROLLERS
weatherApp.controller('homeController', ['$scope', 'cityService', function($scope, cityService) {

    $scope.city = cityService.city;

    //We are going to watch if the city text box is changed and updated the service
    $scope.$watch('city', function()
    {
        cityService.city = $scope.city;
    });

}]);

weatherApp.controller('forecastController', ['$scope', '$resource', '$routeParams', 'cityService', '$log', function($scope, $resource, $routeParams, cityService, $log) {

    $scope.city = cityService.city;

    $scope.weatherApi = $resource("http://api.openweathermap.org/data/2.5/forecast/daily?APPID=fcbc7173d3b696941002572f3f807129",
        {callback: "JSON_CALLBACK"}, {get:{method: "JSONP"}});

    $scope.days = $routeParams.days || 2;

    $scope.weatherResult = $scope.weatherApi.get({q: $scope.city, cnt: $scope.days})
    $log.log($scope.weatherResult);

    $scope.convertToCelsius = function(degC)
    {
        return Math.round(degC - 273.15);
    }

    $scope.convertToFahrenheit = function(degF)
    {
        return Math.round(1.8*(degF - 273) +32);
    }

    $scope.convertToDate = function(toDate)
    {
        return new Date(toDate*1000);
    }
}]);

这是forecast.htm脚本:

<label class="label label-info">Forecast for {{ city }} </label>
<hr/>
Days: <a href="#/forecast/2">2</a> | <a href="#/forecast/5">5</a> | <a href="#/forecast/10">10</a>
Degree: <button ng-click="convertToCelsius(w.temp.day)" class="btn btn-info">C</button> | <button ng-click="convertToFahrenheit(w.temp.day)" class="btn btn-info">F</button>
<div ng-repeat="w in weatherResult.list">
    <div class="row">
        <div class="col-md-6">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <h3 class="panel-title">{{ convertToDate(w.dt) | date: 'MMM, d, y' }}</h3>
                </div>
                <div class="panel-body">
                    <div class="col-sm-3">
                        {{w.temp.weather.icon}}
                    </div>
                    <hr/>
                    Daytime temperature {{convertToCelsius(w.temp.day)}}
                </div>
            </div>
        </div>
    </div>
</div>

convertToCelsius()工作正常,我试图在ng-repeat中获取convertToFahrenheit()函数,因为w在其中,但仍然没有变化

1 个答案:

答案 0 :(得分:1)

您必须在范围中存储要显示的值,然后根据该变量使用convertToCelsius或convertToFahrenheit。所以在你的app.js中,在forecastController中添加以下行

$scope.display = 'C';

然后像这样更改按钮

<button ng-click="display = 'C'" class="btn btn-info">C</button> | <button ng-click="display = 'F'" class="btn btn-info">F</button>

并显示像这样的临时

Daytime temperature {{display == 'C'?convertToCelsius(w.temp.day):convertToFahrenheit(w.temp.day)}}

显示变量将决定temp是显示为Celsius还是Fahrenheit

希望这有帮助, 约翰