Create shorter nicknames for lengthy reference names within the ng-repeat loop

Is it possible to assign an alias to a long reference name in ng-repeat?

Currently, I have 2 complex objects where one acts as a grouped index for the other. Although the ng-repeat template code is functioning correctly, it's getting hard to read and maintain, especially considering revisiting it in the future.

If feasible, how can I transform the following structure:

<div ng-repeat="a in apples">
    <div>
        <span>Type</span>
        <span>{{a.category[1].information.type}}</span>
    </div>
    <div>
        Don't you just love <span>{{a.category[1].information.type}}</span> apples. 
        My granny says {{a.category[1].information.type}} apples are the best kind. 
        I would pick {{a.category[1].information.type}} over {{apples[0].category[1].information.type}} apples any day of the week.
    </div>
</div>

to something like this:

<div ng-repeat="a in apples"
     ng-example-assign="ta = a.category[1].information.type">
    <div>
        <span>Type</span>
        <span>{{ta}}</span>
    </div>
    <div>
        Don't you just love <span>{{ta}}</span> apples. 
        My granny says {{ta}} apples are the best kind. 
        I would pick {{ta}} over {{apples[0].category[1].information.type}} apples any day of the week.
    </div>

</div>

The use of ng-example-assign directive was created for illustration purposes. Can anyone confirm if such functionality is achievable with Angular or ng-repeat? It might require a reevaluation of the code structure, but I wanted to inquire here first. Thanks in advance!

Answer №1

If you want to assign values using ng-init, here's an example:

<div ng-controller="MyCtrl">
    <div ng-repeat="s in data" ng-init="address = s.address">
        {{ address }}
     </div>
</div>

Check out the jsfiddle demo here: http://jsfiddle.net/5yfgLgr9/2/

Answer №2

Implementing a directive with an isolated scope is quite simple.

Template HTML

<div assign="fd" source="data[1].items[1].subitems[1]">

Directive

app.directive('assign',function(){
  return {
    scope:{'assign':'@', "source": '='},
    templateUrl: 'testTemplate',
    link:function(scope){
      scope[scope.assign]=scope.source;
    }
  }
});

It's generally recommended to steer clear of naming custom directives with ng to prevent conflicts with potential upgrades or third-party modules that may also use them.

DEMO

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

Determine the presence of a JSON object within a file using JavaScript

Currently, I am developing a mobile app using react-native and have been facing challenges implementing error checking. To store data retrieved from an API in JSON format, I am employing redux along with thunk. At times, the search results yield a JSON res ...

JS selection-dropbox

As someone who is relatively new to JS, I am struggling a bit. The goal of the code is to display items with specific content (ALL, A, B, C). While the code works fine with buttons, I can't seem to get it to work with a 'Dropdown-select', ...

I am encountering an issue where pagination is not functioning correctly while applying filters. Can anyone suggest a

I am currently experiencing an issue with my datatable. The result and pagination function correctly, however, when I apply a filter, the pagination does not adjust accordingly. This seems to be a common problem on this type of page. Even after filtering, ...

Develop a game timer using CreateJS

Looking for advice on the most effective method to create a timer clock using Createjs. I've attempted to reference HTML elements with DOMElement in the past, but encountered difficulties. Essentially, I need to display a timer within a container so p ...

Looping through items with ng-repeat and creating a submenu with M

When constructing a menu from a JSON file with submenus using the JQuery.mmenu plugin, I encountered an issue when trying to implement the submenu structure with ng-repeat. The raw HTML approach worked perfectly, but as soon as I introduced ng-repeat to dy ...

Execute a PHP script to retrieve data from a database based on the content of a text element

I'm currently working on a web-based system and I'm wondering if it's possible to retrieve a Name based on the number entered in a text box. Here is what I have attempted so far, but I know it's not functioning properly. Is there an alt ...

Is there a way to determine the number of clicks on something?

I'm attempting to track the number of times a click event occurs. What is the best method to achieve this? There are two elements present on the page and I need to monitor clicks on both of them. The pseudo-code I have in mind looks something like ...

Exploring Vue.Js and Airtable: Mastering the Art of Filtering

I am currently working on a project using vue.js to showcase information from an Airtable database. I am looking to implement a filtering functionality for the data displayed. The table contains education details, including subject information like biology ...

What is the best way to add a picture using React and Next.js?

Being a novice in React and Next, I recently embarked on a project that involves uploading a profile picture. However, every time I try to upload an image, an error pops up. Error: The src prop (http://localhost:3333/files/ SOME IMAGE.jpg) is invalid on n ...

One effective way to redirect after a PUT request and display a one-time message

Here's what I'm aiming for in terms of desired behaviour: A user navigates to a password change web page. The user completes the form and it is sent via PUT request to our REST server. Upon successful completion, the user is redirected to their ...

When it comes to React, an unspoken truth is that undefined could potentially cause issues

I have been attempting to iterate through an array of data, following a guide without much success. The structure of the data file is as follows: import React, {Component} from 'react'; export default [ { id: 1, lk:593458, ld:18033, status: &ap ...

Encountering issues when attempting to install vue-cli on a new project

After creating an empty project, I attempted to install vue-cli using the command npm install -g @vue/cli. However, during the installation process, I encountered the following errors and warnings from the interpreter: npm WARN read-shrinkwrap This versi ...

how to set up automatic login for a user after changing their password using passport-local-mongoose

Trying to automatically log in a user, but encountering an issue with the current 'update' function that looks like this exports.update = async (req, res) => { const user = await User.findOne({ resetPasswordToken: req.params.token, re ...

Transform the JSON structure with the power of JavaScript

Seeking assistance in converting the following array of JSON using either javascript or jquery: [ [{"day1":10,"day2":154,"day3":24,"day4":48,"day5":154,"day6":48,"day7":154,"name":"Packet"}], [{"day1":10,"day2":154,"day3":24,"day4":48,"day5":154,"day6":4 ...

What are the steps for creating a new npm package based on an existing one?

I'm a newcomer to the node ecosystem and the npm package system. In my redux/react web app, I currently make use of the photoswipe package alongside react-photoswipe. Recently, I decided to extend the functionality of the photoswipe package by making ...

Supporting traditional IE document compatibility within an iFrame

Currently, I am facing an issue with my web application which is being used in an iframe by a web portal (SAP enterprise portal). The portal is responding with the header x-ua-compatible IE=EmulateIE7, causing complications for my AngularJS-based applicati ...

I am currently utilizing react-admin, however, I am encountering an issue with the heavy build causing errors

Currently, I am working on developing the frontend using react-admin. Initially, I intended to utilize NextJS and found a helpful reference article. Reference: When attempting to run next dev, everything worked smoothly without any errors. However, when ...

Tips for accessing JSON values in JavaScript

How can I extract values from a JSON object in JavaScript? Below is the code snippet I've tried: var obj={"0.5":0.009333, "0.21":0.048667,"0.31":0.070667}; var value =0.21; var p=0; for(i=0; i<= obj.length ;i++){ if(value== obj[i]) ...

Uncover hidden content by clicking a button with JavaScript

I need help with creating a function that hides additional content until a button is clicked. Currently, my script displays the content by default. How can I modify it to hide the content by default and only reveal it when the button is clicked? functi ...

Unable to postpone the utilization of data in Vue until after retrieving the value from the database

I am facing an issue where I need to compare a string obtained from Firebase in document.check1 with specific strings (hardcoded in the function below) and display content accordingly. Currently, I know how to trigger this comparison on button click, but I ...