Utilize an object model property as an array key for filtering in the AngularJS and MEANJS framework

I am currently working on a MEANJS application that includes a CRUD module for countries. When I select a country from the default list view, it redirects me to the view-country.client.view page. In order to enhance my model, I have introduced a field named currencycode. My goal is to display the exchange rate by matching the currencycode field of the Country model with data imported from a JSON file.

//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        
            });
        };

Currently, the integration with the JSON import functions correctly, returning the appropriate rate when including the country code (e.g., 'AFN' for Afghanistan). However, the challenge arises when attempting to dynamically embed the {{country.currencycode}} within the 'AFN'. Despite experimenting with filters, I have not been able to achieve the desired outcome. Essentially, I aim for each click on a country to only display the corresponding rate through utilizing the model property as a string to retrieve the correct object from the array. After spending an entire day scouring forums without success, any assistance would be greatly appreciated. Thank you.

Answer №1

So if I'm getting this right, you prefer not hardcoding 'AFG' in this section?

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

If that's what you're looking for, have you experimented with this approach?

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving information from a Django model and showcasing it in an HTML table through AJAX

After numerous attempts, I remain unable to successfully display a HTML table using AJAX from a JsonResponse in Django. The closest progress I've made so far is seeing a response in the network console: {"products": "[{\"model\": \"pr ...

Guide on displaying the AJAX response within an HTML or JSP element pulled from a database

description of image 1description of image 2How can I display an AJAX response from a database (map) on a JSP screen? I am able to retrieve the response in the browser console but unsure how to visually render it on the JSP page, like in a table or any ot ...

Where can I locate the list of events supported by CKEditor 4?

Looking for the list of available events I attempted to locate the event list in the official documentation, but unfortunately came up short. I resorted to searching through the source code using .fire("/s+") to identify all available events. However, thi ...

Repair the navigation bar once it reaches the top of the screen using ReactJS

I have a webpage that contains specific content followed by a bar with tabs. My goal is to have this bar stay fixed at the top of the screen once it reaches that position while scrolling down, and only allow the content below the fixed bar to continue scro ...

The sequence of HTML attributes

Could there be a subjective angle to this question (or maybe not)... I work on crafting web designs and applications using Visual Studio and typically Bootstrap. When I drag and drop a CSS file into an HTML document, the code generated by Visual Studio loo ...

Sticky box fails to maintain position as header scrolls

I am looking to create a Sidebar that sticks to the window while scrolling, but stops when it reaches the footer. I have managed to get it partially working, but there is a small issue that I can't seem to solve. Test it live here: Everything seems ...

Unveiling the Secrets of AJAX Requests with Enigmatic Strings Attached to URLs

My goal is to keep track of cricket scores on scorespro/cricket by utilizing browser AJAX requests. By analyzing the network traffic in Google Chrome, I have noticed that my browser sends out requests in this format: Whenever I click on the response withi ...

RestKit throws an error while trying to map data

Currently, I am developing an application that fetches 3 JSON objects from a Wordpress site. These JSON objects are then mapped into 3 different classes: Products, Locations, and Categories. While the mapping for Products and Categories is functioning corr ...

Tips on how to loop through every piece of information within a table

<table class="table_style" id="table"> <thead> <tr> <th>Id</th> <th>Name</th> <th>Email</th> <th>Phone</th> </tr> </thead> <tbody> ...

Guide on transforming a JSON object within Lighthouse GraphQL into a GraphQL response

Struggling to figure out how to convert a JSON data source into a GraphQL query on a Laravel server with Lighthouse? You're not alone. My setup involves an API that returns JSON data, and I'm looking for guidance on mapping this data to a GraphQL ...

Click on Angular ng-repeat to dynamically insert data using ng-click

When using $http.get to fetch PHP data in ng-repeat, everything works fine. However, I encounter an issue when attempting to use ng-click to add PHP data from a second page into the ng-repeat, but it is not functioning correctly. Do you have any suggestio ...

Looking for assistance with changing the class of a span when a radio button is selected

I'm facing an issue with toggling the class of a <span> when a radio button is clicked. <html> <head></head> <style type="text/css"> .eHide { display:none; } </style> <script type="text/javas ...

Retrieving precise information from the backend database by simply clicking a button

As a new full stack programmer, I find myself in a challenging situation. The root of my problem lies in the backend table where data is stored and retrieved in JSON format as an array of objects. My task is to display specific data on my HTML page when a ...

The show more/show less link for a long jQuery paragraph is malfunctioning

I encountered an issue while coding where the "read more" link works correctly, but the "show less" link does not. Despite my efforts, I cannot seem to identify the error. Within this code snippet, there is an anchor tag with class="show-less" that I am u ...

Using PHP to decode a JSON array (Troubleshooting issues with Ajax JSON POST to PHP)

I am new to PHP and I have a jQuery/JavaScript based application that involves finding nearby places and presenting the results in a sorted order. I am attempting to move the sorting function from the HTML page to server-side PHP. This process includes ...

How to position Angular Material menu on the right side

I'm currently working with Angular Material and I have a navigation bar (shown below). I've placed a md-menu inside the nav bar in order to create a dropdown menu on the right side, similar to Bootstrap's navigation bar. How can I relocate t ...

Ways to adjust the visibility of a div element multiple times using javascript and css

I implemented a popup feature in my application: <div id="modal"></div> let modal = document.getElementById('modal') modal.innerHTML = ` <button class="close-button" onclick="close_modal()" ...

Traverse a collection of nested objects containing arrays as their values

Consider the following object: { "apples": [ "one", "two" ], "oranges": [ "three", "four" ] } If I want to find the value four within this object, how can I do so efficiently? Would a loop work, like the one shown below? for (var ...

Ember.js - The Veggie Power-Up: [object Object] encountered an error with:

I've recently started using ember and have set up a sandbox to experiment with it. An interesting issue arises when I run the ember s command - an error pops up, but here's the strange part: the error only occurs when I have Sublime Text open (!? ...

What is the functionality behind object inheritance using the clone() method in this implementation?

I am currently exploring kangax's blog post titled Why ECMAScript 5 still does not allow subclassing an array. In this article, he introduces a unique approach to subclassing that deviates from the traditional prototypal method. BaseClass.prototype = ...