Utilizing ng-repeat and ng-controller for displaying individual items

I am currently working on assigning a controller to each item in a list in order to manipulate the data of each list item individually. The information displayed will vary for each item.

<div ng-repeat="item in items" ng-controller="ItemController">
    {{item.name}}
    <select ng-model="selectedSystem" ng-options="system.name for system in systems">
    <button ng-click="dosomething()">do something</button>
</div>

Is there a way to access the item within the ItemController? I want to be able to customize the list of systems based on the specific item being accessed inside the ItemController.

Is it possible to move all the content inside the divs into a template when using ng-repeat?

Thank you!

Answer №1

Thanks to the directive solution recommended by (@charlietfl and eddiec), I was able to successfully resolve the problem.

Answer №2

In my experience, it's not recommended to add multiple controllers for each item in an array. It's important to consider where your items are defined. You likely have a parent controller where the array is requested from a service or statically defined.

If you need access to the item inside the item controller, you can simply pass it into your "doSomething()" function. Here is an example of how you could structure your Controller:

$scope.items = [
  {
    name: "Name1",
    systems: [
      {name: 'System 1'},
      {name: 'System 2'},
      {name: 'System 3'}
    ]
  },
  {
    name: "Name2",
    systems: [
      {name: 'System 4'},
      {name: 'System 5'},
      {name: 'System 6'}
    ]
  }
];

$scope.doSomething = function(item){
  console.log(item);
}

And here is the corresponding HTML code:

<div ng-controller="someController">
  <div ng-repeat="item in items">
      {{item.name}}
      <select ng-model="selectedSystem" ng-options="system.name for system in item.systems"></select>
      <button ng-click="doSomething(item)">do something</button>
  </div>
</div>

This setup will display a list of drop-down menus with corresponding systems and buttons that print out the selected item. Avoid using a controller per item and remember to maintain proper indentation and use camelCase for functions for readability.

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

Using Typescript, you can specify an array of type <T> within an object

Having a background in JS, I am currently exploring TS. My goal is to create an object with a single field, which is an array of strings, while ensuring strong typing. let container = { items: ['Item 1'] } container.items[0] = 3; // This is i ...

Is it possible to stream audio and video independently within the same video file?

<video id="video1" width="320" height="176" controls="controls"> <source src="mov_bbb.mp4" type="video/mp4"> <source src="mov_bbb.m4a" type="video/m4a"> </video> I am looking to enhance this video by incorporating an audi ...

Obtaining a JSON reply using Ember

Exploring new possibilities with Ember js, I am eager to switch from fixtures to using an API. Below is the code I have implemented to fetch the data: App.ItemsRoute = Ember.Route.extend({ model: function() { return $.getJSON('http://som ...

Choose the dropdown item depending on the related text

I am currently working on a drop-down menu where each option has a value and associated text. The selected option is then displayed row by row in a table, with an edit button next to each row that allows the user to change the selection. I am trying to imp ...

How to dynamically add table rows and cells with JavaScript using a single selection input

I am working on a project that involves a selection input with around 5 options. Additionally, there is an empty table that follows this format: <table> <tr> <td>X (for deletion)</td> <td>Option name</td> ...

Is there a way to position two Grid elements to the left and one to the right in Material UI, especially if the first Grid element has a specified width

I'm currently using Material UI and have a Grid layout with three items. The left item needs to have a fixed width of 240px and be aligned to the left. The middle item should be left justified and can have any width, containing buttons that I've ...

Sending the value of a react select component from a React.js application to Firebase

To implement dropdown selections that appear upon click, I utilized react-value. Within my code, I hardcoded some static values for the dropdown options as shown below: import React, {useMemo, useState} from "react" import Select from 'react ...

Angularjs 2 Error: Unable to access the 'infos' property of an undefined object using the Http Client

I've been working on an AngularJS app for about a week now, developing a backoffice application for my service. My main challenge lies in using data retrieved from a remote server. I have 4 HTTP GET requests in my app - 2 of them fetching lists of us ...

What is the best way to extract the refresh token from the headers while utilizing node.js?

I currently have two tokens, an accesstoken and a refreshtoken, in the authorization section. The accesstoken is located in the front while the refreshtoken is in the back. I successfully obtained the accesstoken, but now I need to filter out the refresht ...

Tips on showing a response in an HTML node with Node.js and AngularJS

Just starting out with NodeJS and AngularJS. My goal is to fetch a response from an external site and display it in Angular JS. I managed to send the request and receive a response, but on the UI JSON appears as a string with forward slashes "\". For ...

Service in AngularJS with multiple functions

I seem to be encountering an issue with the service in my AngularJS application. I am receiving the following error: Uncaught SyntaxError: Unexpected identifier Error: [$injector:unpr] Unknown provider: NewContactDataProvider <- NewContactData This is ...

What is the best way to include a property with a name in quotes to an object after it has already been declared?

Is there a way to include a property with a quoted name to an object after its declaration? Here's a simplified scenario: var Obj = {} Instead of: Obj.dog = "Woof!"; I want to achieve: Obj."dog" = "Woof!"; Similar to: var Obj = { "dog" : " ...

Error in HTML5 video: Unable to access property '0' as it is undefined

I am trying to create a simple block displaying an HTML5 video tag. I want the ability to play different videos along with their titles from a JSON file by using previous and next buttons. Clicking the next button should play the next video, and the same g ...

How to stop sorting in ng-repeat using AngularJS

With AngularJS, I am utilizing a json array within an ng-repeat, and I have noticed that it arranges the input differently than how it is structured in my html. Is there a way to prevent this ordering from occurring? Below is the code (View it on Plunkr): ...

Which is better to use: angular.element or $?

Are angular.element and $ essentially the same in AngularJS? Which one is considered best practice and offers better performance? ...

Using ES6 Generator in conjunction with the $http service

I'm exploring the ES6 generator in combination with Angular's $http service on the client side. My goal is to utilize the $http service without relying on callbacks, if achievable. For example: var gen = function* () { var test = yield $http ...

The magic of jQuery's prop() method meets the powerful optimization of the

I have been using the google closure compiler and encountered a warning that doesn't make sense to me. I am trying to check if a radio button is checked with the following code: // ==ClosureCompiler== // @output_file_name custom.js // @compilation_le ...

Learn how to showcase information stored in a JSON file within a list format and how to retrieve specific elements within a JavaScript object

I am struggling with populating a list in my Vue 3 app using a validated JSON file containing all the languages of the world. My lack of experience is making it difficult for me to access the inner parts of an object, so I need some guidance on that. JSON ...

Utilizing kebab-case conventions for CSS class names in CSS/SASS modules within a Next.js environment

Currently, I am utilizing SCSS modules to style my components in React/Next.js but I can't seem to grasp how to import kebab-case classes. As of now, I find myself writing all my SCSS classes in camelCase format, which is not the most efficient appro ...

Encountering an Error during the Installation of MapBox SDK in React Native

After creating a React Native project with Expo using expo init MapTry, I encountered an issue while trying to install the MapBox library. Despite following the installation guide at https://github.com/rnmapbox/maps#Installation, I faced an error message w ...