Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have a MEANJS app with a CRUD module of countries. When I click on a country from the default list view, it brings me to the view-country.client.view. I have added a field to my model for 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.

//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. 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. 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. I have been in forums all day and cannot figure this out. Thank you in advance.

share|improve this question
    
If I understood correct, you don't want 'AFG' to be hardcoded here: {{exchangeRates.rates['AFN']}}? If that's the case have you tried {{exchangeRates.rates[country.currencycode]}}? – Ivan May 15 at 21:03
    
Wow, I had ['country.currencycode'] but never tried it without the '' after trying so many other combinations - what an embarrassing oversight. You were correct - it worked as you suggested. Post as an answer and I will accept! Thank you! – stvn May 18 at 1:43

1 Answer 1

up vote 0 down vote accepted

If I understood correct, you don't want 'AFG' to be hardcoded here?

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

If that's the case have you tried this?

{{exchangeRates.rates[country.currencycode]}}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.