Determine the value from an object in the view by evaluating a string in dot notation

Seeking assistance with a recurring issue I've encountered lately.

Imagine having two objects in AngularJS:

$scope.fields = ['info.name', 'info.category', 'rate.health']

$scope.rows = [{
    info: { name: "Apple", category: "Fruit"},
    rate: { health: 100, ignored: true}
},{
    info: { name: "Orange", category: "Fruit"},
    rate: { health: 100, ignored: true}
},{
    info: { name: "Snickers", category: "Sweet"},
    rate: { health: 0, ignored: true}
}]

I aim to display a table on a view that only exhibits the fields listed in $scope.fields. While flattening the table using JavaScript is an option, I'm curious if there's a method to achieve this by converting the dot notation into the path.

I have included a JSFiddle link to showcase the issue I am encountering:

JSFiddle: http://jsfiddle.net/7dyqw4ve/1/

I have also experimented with a suggested approach but utilizing functions in the view is not recommended: Convert JavaScript string in dot notation into an object reference

If you have any insights or solutions, your input would be highly valued. Thank you.

Answer №1

To utilize angular's built-in evaluator, you can access the $eval property on the scope (not to be confused with the vanilla eval) to evaluate any angular expression (rather than JavaScript as regular eval does). Angular uses this internally for evaluating expressions while handling bindings.

For example:

{{$eval(field, row)}}

You could also do:

<th ng-repeat="field in fields">{{$eval(field, row)}}</th>

Alternatively, you can encapsulate the logic within a controller and expose it through a method on the scope.

<th ng-repeat="field in fields">{{getValue(field, row)}}</th>

and

$scope.getValue = function(exp, row){
    return $scope.$eval(exp, row);
}

See Demo

You may also consider using the $parse service by injecting $parse.

$scope.getValue = function(exp, row){
    return $parse(exp)(row);
}

and

<th ng-repeat="field in fields">{{getValue(field, row)}}</th>

Check out the Demo

Answer №2

A slight modification of PSL's solution that also does the job:

<th ng-repeat="field in fields">{{$eval('row.'+field)}}</th>

The expression $eval('row.'+field) initially gives us row.info.name for fields[0], which then translates to Apple through the binding {{row.info.name}}.

Find it on JSFiddle: http://jsfiddle.net/tbmpls/xhon86p7/8/

This method is straightforward, easy to comprehend, I believe.

From what I can tell, the performance should be nearly identical to a direct {{row.info.name}} binding because generating the string should be very quick - but I am curious to hear any opinions on this matter.

(On another note, this approach also functions within an ng-repeat where your list is nested within an object like :

<span ng-repeat="val in $eval('myobj.'+myPathInDotNotation) track by $index">

though as with all things related to ng-repeat, it would be wise to verify the performance of this)

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

When using Restangular, the initial call will return an array while subsequent calls will return an object

In my AngularJS application, I have a factory called 'UserSrvc' which is responsible for handling communication with a RESTful backend to manage user accounts using Restangular: (function () { 'use strict'; angular .mo ...

Could not locate the provider: $stateProvider

It's puzzling to me why this code is not recognizing the $stateProvider. Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to: Error: [$injector:unpr] Unknown provider: $stateProvider This is a simple example of a module: ( ...

JWT - Effective strategies for enhancing the user experience for a returning logged-in user

My client authentication system involves storing a JWT in `localStorage` once the user is verified. However, I'm not satisfied with the current user experience when a returning user is redirected straight to a new page without warning. window.locatio ...

A dynamic image carousel featuring multiple images can be enhanced with fluid movement upon a flick of

I am trying to enhance this image slider in HTML and CSS to achieve the following objectives: 1. Eliminate the scroll bar 2. Implement swipe functionality with mouse flick (should work on mobile devices as well) 3. Make the images clickable .slider{ ove ...

Is it possible to utilize the expose method to retrieve additional reactive variables and computed properties similar to methods in Vue 3?

Switching my application from Vue 2 to Vue 3 has been quite a journey. I've utilized the Composition API to refactor my previous render function into the setup hook. One interesting discovery was the ability to expose methods using context.expose({}). ...

JavaScript DateTimePicker onChange event malfunctioning

Issue with JS datetimepicker onChange event not functioning. Attempts have been made using OnSelect, OnClose, OnChange, OnHide, OnChangeMonth to trigger the datetimepicker event, but none of them seem to be effective. <input type="text" class="form-co ...

Ways to create a rounded pane for Highchart's Solidgauge

I've noticed that my arcs have rounded edges, but the pane is flat. Is there a way to make the pane rounded as well? I am currently using Highcharts v9.1.2. I tried using "stroke-linejoin: "round"", but it didn't work. https://i.sstati ...

Set onfocus for DOM elements dynamically

I have a question regarding assigning onfocus or similar functions to DOM elements during runtime. Here is the scenario: <input type="text" id="box" onfocus="inputFocused()"/> Now, in a runtime assignment case: <input type="text" id="box"/> ...

Deciphering the issue: "Error: ng:areq"

Hello, I have developed a sample program using Ionic framework. In the app.js file of my Ionic project, I defined a variable as follows: (var itemCheck=angular.module('Shop',['ionic','starter.controllers']);) Below is a snipp ...

The function req.checkBody does not exist

Currently, I am following the guidance of the Mozilla Express tutorial (https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/forms). However, as I reached the section involving express-validator, I encountered a persistent error messag ...

Enhance your AngularJS skills by incorporating multiple conditions into the ternary operations of ng-class

I am struggling to apply multiple classes when the condition in the ng-class attribute evaluates to true. Here is the code I have attempted so far, but it doesn't seem to be working: <div class="col-md-4" ng-mouseover="hoverButton=true" id="plai ...

Is there a way to transmit React code using Express?

Currently, I'm in the process of creating a coloring demonstration. Initially, I had an SVG file at hand, but I made the decision to utilize svgr for its conversion into a React component. This strategy will offer me the flexibility to easily modify i ...

Organize Javascript objects based on their dates - group them by day, month,

I've scoured the internet for examples but haven't found a suitable one that accomplishes the simple task I need. Maybe you can assist me with it. Here is an array of objects: [ { "date": "2015-01-01T12:00:00.000Z", "photoUrl": "", ...

Unable to view image on Django and Angular platform

Previously, I encountered an issue with page refreshes. Thanks to this Solution, I was able to resolve the problem. However, after implementing this in the URL pattern, images are failing to load correctly. Whenever I attempt to open the image source in a ...

Retaining UI state across views in Ionic and AngularJS

In my experience with Ionic and AngularJS, I often run into the same issue when working on projects. I manipulate data with various displays such as lists and maps, and when a user clicks on an element, they are taken to a detailed page. It would be benef ...

Confusion arises from the code for processing an Ajax redirect

I finally succeeded in incorporating an Ajax call into my code, but I'm a bit puzzled about how to redirect after the call is made. Below is an example of my script, developed using CodeIgniter: <script type="text/javascript"> function myFunc ...

Utilize Material-UI's <Autocomplete /> feature to conduct searches using multiple parameters

Recently, I started delving into mastering Material UI and something has been on my mind. We are working with an array of objects as follows: const top100Films = [ { label: 'The Shawshank Redemption', year: 1994 }, { label: 'The Godfath ...

Automatically select and pre-fill a list item based on its value using jQuery

I'm a beginner in JQuery. In my application I have the following: Here is my HTML code: <ul id="list"> <li> <a class="selected" value="0" href="#" id="sel">ALL</a> </li> <li> <a hre ...

The event listener for changing radio buttons

Imagine we have two radio buttons <input type="radio" value="val1" name="radio1" onChange="change()"/> <input type="radio" value="val2" name="radio1" onChange="change()&quo ...

Can a robust web application be developed using Kotlin in Node.js?

With the recent release of Kotlin 1.1, it is now possible to compile Kotlin projects into JavaScript as a target, allowing for full JavaScript compilation. Can a Node.js application, like an express webserver, be developed entirely using Kotlin code? As s ...