Angular.js 研究一个元素,但不研究一个非常相似的元素

Angular.js working on one element, but on one very similiar not

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

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

我是 Angular 的新手,这让我很困惑,如果有人能指出我做错了什么,我将不胜感激。

验证您 selects::

 <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>

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

并且您将其附加到 url:

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

只需尝试更改您的第二个 select,如下所示,它应该可以工作。

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

使用变量存储您从 api

返回的目的地
var destinations = {};
...

在您的回复中 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;
                        }
                    });

                }
            });

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