Updating information dynamically in a controller based on an ng-repeat from a different controller in AngularJS

To enhance comprehension, I've created a straightforward example.

For this scenario, ng-repeat is used to call a template that has its own controller. However, the controller of the template requires injected data from a service for each ng-repeat iteration. In this case, it should be the answer to the query at hand. How can this be accomplished?

<div ng-repeat="fighter in fighters">
    <div ng-if="fighter.state">
        {{fighter.question}}
        <div ng-include="'template.html'" ng-controller="Controller2"></div>
    </div>
    <br>
    <br>
</div>

If you wish to view the complete working demo, visit this Plunker:

DEMO PLUNKER

Answer №1

To start, open your template.html file and create a ng-init function to pass the object.

<div ng-controller="Controller2" ng-init="getAnwser(fighter)">
  <div>
    <!-- Insert question from database here -->
    <input type="text" ng-model="answer"  />
  </div>
  <div>
    <button ng-click="checkAnswer()">
      ANSWER
    </button>
    <div ng-show="showAnswer">Correct</div>
  </div>

</div>

Next, in your controller2, implement the function as follows:

$scope.getAnwser = function(obj){
  console.log(obj.answer)
}

For a demonstration of this functionality, you can check out this 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

Guide on how to load just the content section upon clicking the submit button

Here is an example of my code: <html> <head> <title>Test Loading</title> </head> <body> <div id="header"> This is header </div> <div id="navigation" ...

When utilizing CKEditor in conjunction with ExpressJS, HTML tags may be displayed in the browser

Check out my app.js code below: <!DOCTYPE html> <html lang="en> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initial-scale=1.0> <meta http-equiv="X-UA-Compatible" content="ie= ...

What sets 'babel-plugin-module-resolver' apart from 'tsconfig-paths'?

After coming across a SSR demo (React+typescript+Next.js) that utilizes two plugins, I found myself wondering why exactly it needs both of them. In my opinion, these two plugins seem to serve the same purpose. Can anyone provide insight as to why this is? ...

What is the best method for styling arrays displayed by React in version 15 using CSS?

React v15 has made a change where elements of arrays in JSX are now rendered as <!--react-text:some_id-->text<!--/react-text--> instead of spans. I've searched for a solution but haven't been able to figure out how to apply CSS styles ...

Tips and tricks for bypassing the annoying "save as" popup while downloading files remotely using SauceLabs

Running a protractor spec from a Jenkins job, connecting to SauceLabs to click a button for downloading a PDF file, and verifying the successful download. Struggling to prevent the chrome browser from displaying a "Save As" prompt when using an absolute pa ...

AngularJS retrieves entire video file for HTML5 video tag

I am currently developing a web application with AngularJS where the main page showcases five videos for the user to choose from and play. However, I have encountered an issue where all source video files are being downloaded for each HTML5 video element d ...

The React component using createPortal and having its state managed by its parent will remain static and not refresh upon state modifications

I am facing a problem that can be seen in this live example: https://stackblitz.com/edit/stackblitz-starters-qcvjsz?file=src%2FApp.tsx The issue arises when I pass a list of options to a radio button list, along with the state and setter to a child compon ...

The modification of a controller's attribute does not impact the view

Recently diving into Angular, I've encountered a small issue. Here's what's going on: After creating a module named "myMod", I proceeded to define a directive like so: myMod.directive("myDirective", function () { dir = { restri ...

Issue with ng-pattern validation for email not functioning as expected

Currently, I am utilizing the following pattern for email validation var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3 ...

Azure App Service for Mobile and Web applications

Currently, I am in the initial stages of assessing and designing the potential architecture for a Mobile App (using Xamarin) and a Web App (most likely using Angular). The Azure App Service appears to be a suitable option for hosting these services. Howeve ...

Error message in React Js indicating the absence of a specified file or directory: "Package.json cannot

When I try to run npm start, I encounter the following error - PS D:\React\operations_app_tut> npm start npm ERR! path D:\React\operations_app_tut\package.json npm ERR! code ENOENT npm ERR! errno -4058 npm ERR! syscall open npm ...

JavaScript: What is the concept of overriding function named params?

function retrieveData({item1 = "blue", item2 = 7}) { console.log('theItems'); console.log(item1); console.log(item2); } retrieveData( { item1: 'pink', item2: 9 } ); I've come across conflicting i ...

Issues with Javascript Arrays not adding objects with duplicate values

When objects have arrays with the same values, only one of them is considered. For example: data[2018][2][25] <-- this one gets ignored by the object data[2018][2][22] Sample Code: var date = new Date(); var data = {}; <?php $eventsNum = 3> &l ...

How do I make a component in React delete itself from a page when its internal state changes?

For instance: {!loading ? (data.map((v, i) => { return <Card key={i} title={v.name} image={v.pictures.sizes[4].link}} /> }) These cards are displayed as a series of components on the main screen. Each card ...

JavaScript is unable to identify the operating system that is running underneath

Even though I am on a Windows system, the browser console is showing that I am using Linux. function detectOS() { const userAgent = navigator.userAgent.toLowerCase(); if (userAgent.includes('win')) { return 'Windows' ...

Accessing the data from an HTML5 slider using JQuery

Having some difficulty fetching a value from an HTML5 slider using JQuery. Here is my code snippet: JQuery Code: // Getting the value from slider one $("#submit").on("click", function(evt) { var sliderValue = $('#slider01').attr('value ...

Rendering data from an API using v-if

Could you help me change the tag that currently displays true or false? I want it to show "FREE" if the event is free and "PAID" if it's not. Check out the Eventbrite API here The response I'm receiving from data.events.is_free is in boolean fo ...

Column Arrangements in a Table

Is there a method to insert a new column into a table using the jQuery plugin DataTables? ...

Can JavaScript be used to determine if any code has been executed from the browser's console?

I'm currently developing a JavaScript game and I want to prevent cheating. Is there a way for me to track and record commands entered in the JavaScript console? Here's a basic outline of what I have in mind: consoleCommands = ""; window.console ...

Tips for enhancing undo/redo functionality when working with canvas drawings in React

Currently, I am working on implementing undo/redo functionality for html-canvas drawing on medical (.nii) images in a React application. The images consist of slices stored in a Uint8ClampedArray and usually have dimensions around 500 (cols) x 500 (rows) x ...