简体   繁体   English

AngularJS + MEANJS-使用对象模型属性作为过滤器的数组键

[英]AngularJS + MEANJS - Use object model property as array key for filter

I have a MEANJS app with a CRUD module of countries. 我有一个带有国家/地区CRUD模块的MEANJS应用程序。 When I click on a country from the default list view, it brings me to the view-country.client.view. 当我从默认列表视图中单击一个国家时,它将带我到view-country.client.view。 I have added a field to my model for currencycode. 我已在模型中添加了一个字段以输入currencycode。 When I click the country it will bring me to a page where I want it to bring up the exchange rate from a json by matching the currencycode field of the Country model with my data from the json import. 当我单击国家/地区时,它将带我到一个页面,我希望它通过将Country模型的currencycode字段与来自json导入的数据进行匹配来调出json的汇率。

//view-country.client.view

<section data-ng-controller="CountriesController" data-ng-init="findRate()">
 <div class="col-lg-3 col-md-6 col-sm-12">
    <div class="panel panel-dark">
        <div class="panel-heading">Currency</div>
        <div class="panel-body">
        {{country.currencycode}}
            <p>The exchange rate is {{exchangeRates.rates['AFN']}}
            </div>
         </div>
   </div>
</section>





//findRate() for data-ng-init in CountriesController

$scope.findRate = function() {

            $scope.country = Countries.get({ 
                    countryId: $stateParams.countryId
                });

            $http.get('http://localhost:3000/rates.json').
            success(function(data) {
                $scope.exchangeRates = data        
            });
        };

Everything is working with the JSON import and if I include the countrycode (in this example 'AFN' for Afghanistan), it will return the correct rate. 一切都在使用JSON导入,如果我包括国家代码(在此示例中为阿富汗,则为“ AFN”),它将返回正确的汇率。 The {{country.currencycode}} brings back AFN from my MongoDB but if i try and embed the {{country.currencycode}} where 'AFN' is, it will not work. {{country.currencycode}}从我的MongoDB中带回AFN,但是如果我尝试将{{country.currencycode}}嵌入到'AFN'所在的位置,它将无法正常工作。 I have tried looking at filters but I cannot get those to work. 我曾尝试查看过滤器,但无法使用它们。 Basically, anytime I click a country, I want it to show only the rate for that country by using the model property as a string for getting the correct object from my array. 基本上,每当我单击一个国家/地区时,我都希望它通过使用model属性作为字符串从该数组中获取正确的对象来仅显示该国家/地区的汇率。 I have been in forums all day and cannot figure this out. 我整天都在论坛上,无法解决这个问题。 Thank you in advance. 先感谢您。

If I understood correct, you don't want 'AFG' to be hardcoded here? 如果我理解正确,那么您是否不想在此处对“ AFG”进行硬编码?

{{exchangeRates.rates['AFN']}}

If that's the case have you tried this? 如果是这样,您是否尝试过?

{{exchangeRates.rates[country.currencycode]}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM