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

Resetting the check status in html can be accomplished by using the checked

I am currently working on a popup feature in HTML <div id="term_flags" class="term_flags"> <div class="modal-users-content flagsContent"> <div class="modal-users-header"> <span class="close" ng-clic ...

Instructions for capturing multi-dimensional arrays using forms in Vue

I have a snippet of code that looks like this: <div class="form-item"> <label for="list-0"><?php _e('List 0', 'test'); ?></label> <input name="list[0]" type="text" id="list-0" value=""> </div> &l ...

Is there a way to trigger an animation after the completion of ng-repeat binding?

Check out this plunker that demonstrates an interesting behavior: the class ng-hide is removed from a tag before ng-repeat has finished creating the HTML view of the data. This results in the height of the element not being final when removeClass is trigge ...

Is there a way for me to extract the document title from firebase?

I have been trying to use this component to retrieve data, but I am encountering an issue where I cannot fetch the name of the document "sampleCloudFunction" under CloudFunctionMonitor (see image) from the database and display it. Using docRef.id does not ...

What are some strategies for retrying an Apollo Client query after a failure?

When working with Next.js server-side rendering (SSR), I often encounter a query failure on the first try due to the backend not being fully ready. In such cases, I would like to retry the fetch using ApolloClient. const PREVIEW = { query: MY_PREVIEW_ ...

Change JSON data into a text format

Looking for help with a Java issue I'm having. Despite reading tutorials and asking questions, I'm still struggling to apply the knowledge to solve my problem. Here's the issue: I need to read multiple json files in one place and convert t ...

Unable to eliminate user registration feature with Meteor and React

Exploring the world of Meteor and diving deep into its functionalities, I am currently focused on creating a user login and signup page with personalized control panels for each registered user. Although I have successfully implemented the signup and logi ...

I'm experiencing a lack of feedback while implementing jQuery/AJAX with JSONP

Attempting to perform a cross-domain request using jQuery/AJAX, I have the code below; $.ajax({ url: "http://www.cjihrig.com/development/jsonp/jsonp.php?callback=jsonpCallback&message=Hello", crossDomain:true }) .done(function( msg ) { alert( ...

Guide to setting up a search function with class name filtering

I am currently managing a website with multiple items, each represented by individual div elements. While all items share one common class name, they also have several other class names serving as tags to differentiate them (some tags may overlap between d ...

Learn how to serialize and submit all form components within a specified element using AJAX

I am attempting to serialize and post all form elements that may originate from either within a <form> element, or any other elements such as divs, trs, etc. In essence, my form can be structured in two ways: <form id="frm1"> Name: ...

Issues with voice state updates in JavaScript

I am currently working on setting up a discord bot to send notifications when someone joins a voice chat. While coding in Atom, I encountered the following error: TypeError: Cannot read property 'send' of undefined at Client.client.on (C:\U ...

Testing Angular directives using $window and $event in unit tests

My directive is simple - it overrides click behavior to trigger a full page reload. However, I am struggling to write a Unit Test for this directive. It seems like the injection of $window is not working properly, resulting in an error when running the tes ...

Refreshing Javascript with AngularJS

I'm encountering an issue while starting my angular js application. On a html page, I have divs with icons and I want the background color to change on mouse over. This is working using jquery's $(document).ready(function(){ approach. The conten ...

Unable to view the most recent state

Looking at the code below: export function loginUserRequest() { console.log('ACTION INITIATED'); return { type: LOGIN_USER_REQUEST, }; } we can see the matching reducer: export default function loginReducer(state = initialState, acti ...

Tips for implementing daterangepicker js in an Angular 2 project

I'm currently working on an Angular 2 project and I'm looking to integrate the daterangepicker.js library for a date range picker. If you're not familiar with it, you can find more information about the library here. Here's the HTML co ...

Issue with AJAX request on Internet Explorer versions 8 and 9

This script is functioning properly in Internet Explorer 10 and above, as well as Chrome and other browsers. However, it encounters issues specifically with IE8 and IE9. <table id="table" border="1"> <tbody style="display: table-row-group"&g ...

sending jqgrid post request with JSON payload

My grid data read is set up to use json format. Here is the configuration: url:"devitem.json", mtype: "POST", datatype: "json", ajaxGridOptions: { type : 'post', async : false, error : function() { alert ...

What methods can be used to modify the appearance of the cursor depending on its position?

Is there a way to change the cursor to a left arrow when on the left half of the screen and switch it to a right arrow when on the right half, using JavaScript? I am trying to achieve something similar to what is shown on this website. I attempted to acco ...

Press the Text and Alter Text with React

I'm having an issue with the onClick event using React hooks. My goal is to have the text change to a different one when the user clicks, and then revert back to the original text on another click. For example, clicking "Change First" should switch it ...

What is the process of generating a popup panel without relying on libraries, using JavaScript and CSS?

I'm currently working on creating a popup panel that is centered on the screen with rounded corners (scrollbars are unnecessary) using jQuery min, similar to this example: https://i.stack.imgur.com/0kYO6.png My progress so far: function (package) ...