gpt4 book ai didi

javascript - Angular JS 和 Javascript 的一些问题

转载 作者:行者123 更新时间:2023-11-30 09:52:50 24 4
gpt4 key购买 nike

我是 Javascript 和 Angular JS 编程的新手,我正在尝试使用 Yahoo Finance API 制作货币转换器,但我也想直接在脚本中输入初始值,而无需单击按钮或按回车键,所以我认为使用 Angular JS 会很棒。但它不能正常工作,我认为问题可能出在 function calculate($scope) 中。拜托,你能帮帮我吗?

<html ng-app>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function httpGet(theUrl)
{
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
function currencyConverter(currency_from,currency_to,currency_input){
var yql_base_url = "https://query.yahooapis.com/v1/public/yql";
var yql_query = 'select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20("'+currency_from+currency_to+'")';
var yql_query_url = yql_base_url + "?q=" + yql_query + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
var http_response = httpGet(yql_query_url);
var http_response_json = JSON.parse(http_response);

return http_response_json.query.results.rate.Rate;
}

//The problem starts here?

function calculate($scope)
{
$scope.total= function(){
var currency_from = "USD";
var currency_to = "INR";
var rate = currencyConverter(currency_from,currency_to,$scope.currency_input);
return rate;
};
}
</script>
<script src="js/angular.js"></script>
</head>
<body>
<div ng-controller="calculate">
<div style="float:left;">
<form>
<input type="text" ng-model="currency_input" value="0"/> = &nbsp;
</form>
</div>
<div style="float:left">
{{total()}}
</div>
</div>
<div style="clear: both;"></div>

</body>
</html>

最佳答案

您使用 AngularJS 的代码中存在一些错误。您忘记启动应用程序模块并添加 Controller 。

我做了一些更正并进行了测试,它正在运行:

<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
</head>
<body>
<div ng-controller="MyController">
<div style="float:left;">
<form>
<input type="text" ng-model="currency_input" ng-change="total()" value="0"/> = &nbsp;
</form>
</div>
<div style="float:left">
{{ rate }}
</div>
</div>
<div style="clear: both;"></div>

<script>
angular.module('app', [])
.controller('MyController', function($scope, $http) {

function currencyConverter(currency_from,currency_to) {
var yql_base_url = "https://query.yahooapis.com/v1/public/yql";
var yql_query = 'select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20("'+currency_from+currency_to+'")';
var yql_query_url = yql_base_url + "?q=" + yql_query + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
$http.get(yql_query_url).then(function(response) {
$scope.rate = $scope.currency_input*response.data.query.results.rate.Rate;
});
}

$scope.total = function() {
var currency_from = "USD";
var currency_to = "INR";
currencyConverter(currency_from, currency_to);
};
});
</script>
</body>
</html>

我不建议您使用 jquery 进行 http 调用。请改用 $http!

关于javascript - Angular JS 和 Javascript 的一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35490393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com