Javascript ng更改仅在ng重复内触发一次--角度
我正在连接雅虎天气服务API,并使用Angular制作了一个简单的天气应用程序。我还创建了一个F-C温度转换器,在ng变化的帮助下触发。但是,由于某些原因,这只在初始化时起作用一次。 出于某种原因,我无法理解为什么ng repeat在ng repeat内只发射一次。请参阅下面的代码。任何帮助都将不胜感激 ***我应该提到,当我在名为changedTemperatureTo的函数中放置一个调试器时,当我从F->C转换时,它会触发调试器,但第二次调试器甚至不会从C->F触发。第二次,我的意思是在我第一次从F->C点击单选按钮之后。当我将其反向翻转时,不会发生任何事情-调试器不会触发 视图: {{date}}Javascript ng更改仅在ng重复内触发一次--角度,javascript,angularjs,ng-repeat,Javascript,Angularjs,Ng Repeat,我正在连接雅虎天气服务API,并使用Angular制作了一个简单的天气应用程序。我还创建了一个F-C温度转换器,在ng变化的帮助下触发。但是,由于某些原因,这只在初始化时起作用一次。 出于某种原因,我无法理解为什么ng repeat在ng repeat内只发射一次。请参阅下面的代码。任何帮助都将不胜感激 ***我应该提到,当我在名为changedTemperatureTo的函数中放置一个调试器时,当我从F->C转换时,它会触发调试器,但第二次调试器甚至不会从C->F触发。第二次,我的意思是在我第
<span ng-repeat="temp in temperatureNames">
<input type="radio" name="selectedTemperatureNamed" ng-model="selectedTemperatureName" ng-value="temp" ng-change="changedTemperatureTo(temp)"/> {{temp}}
</span>
<select ng-model="sort">
<option value="cityNameForForecast(td)">City</option>
<option value="high">High</option>
<option value="low">Low</option>
</select>
<input type="checkbox" ng-model="reverse"/> <small>*Check to Reverse Order</small>
<div ><br>
<table class="tg">
<thead>
<tr >
<th class="tg-031e"></th>
<th class="tg-031e"></th>
<th class="tg-031e"></th>
<th class="tg-031e">High</th>
<th class="tg-031e">Low</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="td in forecastAll | orderBy:sort:reverse">
<th class="tg-031e"><a href="#/weather/city/{{cityNameForForecast(td)}}">{{cityNameForForecast(td)}}</a></th>
<th class="tg-031e"><img src="http://l.yimg.com/a/i/us/we/52/{{ td.code }}.gif"></th>
<th class="tg-031e">{{td.text}}</th>
<th class="tg-031e">{{td.high}}</th>
<th class="tg-031e">{{td.low}}</th>
</tr>
</tbody>
</table>
</div>
服务:
weatherApp.factory("weatherService", function ($http) {
'use strict';
return {
doSomething: function ($scope) {
},
getWeather: function (city) {
var url = this.getUrlForCity(city);
return $http.get(url);
},
getUrlForCity: function (city) {
// Weather codes:
var weatherCodes = {'Vancouver': 'CAXX0518', 'Honolulu': 'USHI0026', 'San Diego': 'USCA0982', 'Havana Cuba': 'CUXX0003'}
var city = weatherCodes[city] // Now on can call all cities at once
var yahooAPI = "'http://weather.yahooapis.com/forecastrss?p=";
var format = "'&format=json&diagnostics=true&callback=";
var yql = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D";
var url = yql + yahooAPI+ city + format;
return url;
},
getForecastFromData: function (data) {
try {
var stringified = JSON.stringify(data); // Convert to a string.
stringified = stringified.split("\\n").join(""); // Remove new line '/n'.
var listing = JSON.parse(stringified); // Convert to object.
var forecast = []; // Store 5 day forecast.
var forecastDate = []; // Stores forecast date
for (var result in listing) {
for (var item in listing[result].results) {
for (var day in listing[result].results.item.forecast) {
forecast.push(listing[result].results.item.forecast[day]);
}
}
}
}
catch (error) {
alert("Weather reading error:" + error.name + ": "
+ error.message);
}
return forecast;
},
arrayToCelsius: function (forecastArray) {
for (var i = 0; i < forecastArray.length; i++) {
forecastArray[i]['high'] = this.getCelsius(forecastArray[i]['high']);
forecastArray[i]['low'] = this.getCelsius(forecastArray[i]['low']);
}
return forecastArray;
},
getCelsius: function (fahrenheit) {
var fTempVal = this.getForecastFromData(fahrenheit);
// debugger;
// $scope.celsius = this.getForecastFromData;
var celsius = ((fahrenheit - 32) * 0.56).toFixed(0);
return celsius; // Calculation goes here.
}
}
});
从我所看到的,你最初设定的温度是华氏度
// Temperature
$scope.temperatureNames = ['C', 'F'];
$scope.selectedTemperatureName = $scope.temperatureNames[1];
问题发生在这里:
$scope.changedTemperatureTo = function(temperatureName) {
// debugger;
if (temperatureName == 'C') {
$scope.forecastAll = weatherService.arrayToCelsius($scope.forecastAll);
} else if (temperatureName == 'F') {
/*
You aren't doing anything to the forcastAll variable when the temperature
is F
*/
$scope.forecastAll;
}
};
当temperatureName=='F'时,不会发生任何事情。所以它会从法伦海特转换成塞尔修斯,但回去时什么也不会发生
希望这有帮助 Tnx。好电话!我应该提到,当我在名为changedTemperatureTo的函数中放置一个调试器时,当我从F->C转换时,它会触发调试器,但第二次调试器甚至不会从C->F触发。第二次,我的意思是在我第一次从F->C打开收音机btn后。当我将其反向翻转时,不会发生任何事情-调试器不会触发。
$scope.changedTemperatureTo = function(temperatureName) {
// debugger;
if (temperatureName == 'C') {
$scope.forecastAll = weatherService.arrayToCelsius($scope.forecastAll);
} else if (temperatureName == 'F') {
/*
You aren't doing anything to the forcastAll variable when the temperature
is F
*/
$scope.forecastAll;
}
};