Angular.js在一个元素上工作,但在一个非常类似的元素上工作

时间:2016-08-26 06:38:07

标签: javascript angularjs angular-material

这是我的代码:&#39; http://codepen.io/summerfreeze/pen/NAkdKR&#39;。我在那里使用受限制的API,但你可以通过f.e访问它。使用Allow-Control-Allow-Origin Chrome扩展。当我从两个<select>列表中选择国家/地区时,我的代码应该显示货币,但只有第一个有效。

当应用程序启动时,第一个国家/地区设置为Wielka Brytania(英国),下面的货币设置为GBP。第二个国家设置为Polska(波兰),下面的货币是PLN(应该是)。当我将第一个国家/地区更改为波兰时,以下货币会更新为PLN。但是,当我尝试更改第二个国家/地区时,无论我选择哪个国家/地区,货币都会变为欧元,这就是问题所在

我是Angular的新手并且对我来说非常困惑,如果有人能指出我做错了什么,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

使用变量存储从api返回的目的地

url: "https://www.easysend.pl/api/calculator/countries/" + selcountry + "/destinations"

来自this.destinations = response.data; 的回复

"https://www.easysend.pl/api/calculator/currencies/" + selcountry + "/" + destination + ""

最后,当您收到$scope.currency2 = $scope.currency[0];

的回复时

而不是angular.forEach(this.destinations,function(dest,key){ if(dest.id == destination){ angular.forEach(response.data,function(currencyOption,key){ if(currencyOption.currency_out.id == dest.default_currency.id){ $scope.currency2 = currencyOption; } }); } }); 检查目标变量

中的默认货币
function playMusic(){
  var music = new Audio('musicfile.mp3');
  music.play();
  }

https://codepen.io/anon/pen/NAkmJo?editors=1010

答案 1 :(得分:0)

验证您选择::

 <select ng-options="item.name for item in countries" ng-model="selcountry" ng-change="countryincheck()">
              </select>
                                    <span>do</span>
                                        <select ng-model="destination" ng-change="destcheck()"  ng-options="item as item.name for item in destinations track by item.name">
                                        </select>

在第一个选择中,您将item.name绑定到selcountry,而在第二个选择中,您绑定item (object)destination这将无效,因为项目不是此处的字符串。

你将它附加到网址:

destination = $scope.destination.id;
                    $http({
                        method: 'GET',
                        url: "https://www.easysend.pl/api/calculator/currencies/" + selcountry + "/" + destination + "" })
                    .then(function successCallback(response) {

尝试更改下面的第二个选项,它应该可以正常工作。

<select ng-model="destination" ng-change="destcheck()"  ng-options="item.name for item in destinations track by item.name">
                                            </select>