Strategies for handling ng-repeat with pre-existing items in AngularJS

I am currently facing a challenge where I need to utilize ng-repeat for a list. However, the twist is that I already have pre-existing list items that are being rendered with Django.

Note: In my AngularJS InterpolateProvider, I have configured {[{ }]}.

Sample HTML Code

<ul ng-controller="ListController">
    {% for item in existing_list %}
        <li>
            <span>{{item.firstName}}</span>
            <span>{{item.lastName}}</span>
        </li>
    {% endfor %}

    <li ng-repeat="item in items">
        <span>{[{item.firstName}]}</span>
        <span>{[{item.lastName}]}</span>
    </li>
</ul>

My objective now is to manage these items using ng-controller

app.js Script

function ListController($scope){
    $scope.items = [{firstName: "Bob", lastName: "Smith"}];
}

The main issue I'm grappling with is how to incorporate the pre-existing list items generated by Django into the $scope.items array?

Answer №1

I currently approach this by utilizing ngInit to inject pre-existing data into the scope. This results in something along these lines:

<ul ng-controller="ListController" ng-init='items = {% existing_list.toJSON() %}'>

(apologies, I am not familiar with how to convert objects to JSON)

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

How to Verify if the Second Tab has Fully Loaded in Selenium Testing

Our current automation process involves using selenium. In order to open 2 URLs simultaneously, we have implemented the following approach: I use driver.get to load the first URL, which includes built-in synchronization for page loading. For the second wi ...

What is the best way to determine the total number of classes that come before a specific element

Currently, this is my approach: <script> function Answered(str) { var script = document.getElementsByClassName('Answered')[str]; if(script!==null) {script.setAttribute("style", "");} } </script> <span class=Answered style=" ...

Eliminate the flickering effect on the dropdown menu

Is there a way to eliminate the annoying 'flicker' effect on my menu? Whenever I click on 'Dropdown 1', I notice that Test 1 and Test 2 flicker. My assumption is that this is due to my use of the !important declaration. Any suggestion ...

Javascript is not functioning properly when making an ajax call

Having a bit of trouble with JavaScript not working after an Ajax call. Everything functions normally until new data is fetched from the database or an Ajax call is made. The issue seems to be that the JavaScript doesn't load properly. Any help would ...

Report of transactions containing product name and customer email address

Looking for a method to create a PayPal report that includes both the Item name (or ID) and buyer's email address. This solution can involve using Google script or directly through the PayPal report engine. Appreciate any assistance! ...

Implementing caching for ajax JSON requests can improve performance and optimize data

Looking to optimize my basic Ajax call for parsing a JSON file. I want to avoid hitting the feed every time someone visits the page. Any suggestions on how to implement caching so that the feed is only requested, let's say, once every 2 hours? $(fun ...

MongoDb Mongoose's aggregatePagination feature occasionally displays duplicated documents across various pages

Currently, I am encountering an issue related to pagination in mongodb and mongoose. My goal is to search through a group of Tutors based on certain criteria and have the results paginated and sorted by their updated date. However, the problem arises when ...

Error encountered while unit testing AngularJS with clientWidth usage

I have encountered an issue in my code. The following line is causing trouble: var contentWidth = angular.element(document.querySelector('.content'))[0].clientWidth; While the app runs smoothly, I encounter an error during unit testing: TypeEr ...

This particular function fails to execute outside of the console and only functions properly within it

Currently, I am experimenting with modifying CSS values using jQuery. My goal is to include the width to a .block element, based on the value of its child element. Oddly enough, the function does not seem to work when the page initially loads. However, if ...

Experimenting with asynchronous functions in React using Jest

As a newcomer to unit testing, I am working with two files: RestaurantReducer import * as Const from '../constants/Const' const RestaurantReducer = (state = { restaurantList: [] }, action) => { switch (action.type) { case Co ...

Adjust the color of radio button text with Jquery

Here is a sample of radio buttons: <input type='radio' name='ans' value='0'> Apple <input type='radio' name='ans' value='1'> Banana <input type='radio' name='ans&apo ...

CSS styling does not apply to HTML elements that are dynamically created using JavaScript

A script containing dynamic HTML content is sourced from an external website, injected by RSS feeds. To access the content directly, visit this link. The script needs to be wrapped around HTML tags for front-end display. For example: <div>http://fe ...

Can the default position of the scrollbar be set to remain at the bottom?

I have a select option tag with a scrollbar to view the contents in the dropdown. I am looking for a way to automatically position the scroll at the bottom when an item is selected from the dropdown. jquery code $('document').ready(func ...

Guide on integrating a dynamic element within another element in Angular

Imagine a scenario where we have the following html structure <div [innerHTML]="contentFromAPI | safeHTML"></div> The content retrieved from the api contains multiple HTML elements, resulting in a structure like this: <div> &l ...

What is the best way to create messages that can function seamlessly in both Java and JavaScript environments?

Utilizing a file named messages.properties with JSTL format messaging has been effective for server-side Java and JSP applications. However, when it comes to incorporating JavaScript, the need arises for the same messages in a format that is compatible w ...

Tips for creating auto-folding code in react-ace editor

Hi everyone, I have come across this JSON data that needs to be displayed in the react-ace editor with folded fields. Can anyone guide me on how to achieve this programmatically? This is what my JSON data looks like currently: https://i.sstatic.net/zhfvl ...

What causes the console.log function to behave in this manner?

When using the node.js interpreter, if you run the code: console.log("A newline character is written like \"\\ n \"."); //output will be:- // A newline character is written like "\ n ". However, if you just enter the following in ...

Is there a way to incorporate JavaScript or jQuery into my navigation bar?

I'm working on implementing JavaScript into my navbar. My goal is to create functionality where clicking on a button changes its class to 'active' while the other buttons revert back to their normal state. <ul> <li><a class=&q ...

Utilizing Google Maps with WebGL to place multiple GLTF objects on a vector map based on geographical coordinates

Trying to incorporate 3D objects onto a Google Map overlay view using Three.js The main goal is to display bus stations using 3D objects with specific longitudes and latitudes Attempted to use the '@googlemaps/three' 'latLngAltitudeToVecto ...

Generating a pop-up window upon clicking a specific word within the text

I've been tasked with converting a textbook into a website for my teacher, and I've encountered an issue. Within a div element, there is text in a foreign language. Certain words need to be clickable, triggering a pop-up box with their translatio ...