The ng-repeat directive, when used with options, does not display the object as the returned value

I have a loop "ng-repeat" inside of

<select ng-model="something"></select>

where each option in a list is displayed as an

<option> 

within the select block. My goal is to set "something" (which is an ng-model linked to select) to be the selected object from the list. Currently, when I use "value="{{option}}" as a parameter for option, I receive a JSON object as a String. However, what I actually need is to retrieve an object as an object. Here's how the code appears:

<select ng-model="something">
    <option ng-repeat="option in list" value="{{option}}">{{option.name}}</option>
</select>

This specific requirement can be easily achieved using ng-options, but I also need to include additional "style" parameters based on option.anotherField for each

<option>

Answer №1

Instead of using the value attribute, you have the option to utilize ng-value in order to select the desired object:

<select ng-model="something">
    <option ng-repeat="option in list" ng-value="option">{{option.name}}</option>
</select>

Answer №2

Exploring the concept of using a temp proxy and its connection to the something variable through the utilization of the ng-change($scope.setModel) directive:

(function(angular) {
      'use strict';
      angular.module('app', [])
        .controller('ExampleController', ['$scope', function($scope) {
          $scope.list = [{name:'Tom',age:23},{name:'Max',age:33},{name:'Sam',age:43}];
          $scope.something = $scope.list[0];
          $scope.temp = $scope.something.name;    

          $scope.setModel = function(){
            for(var item of $scope.list)
              if(item.name == $scope.temp){
                $scope.something = item;
                break;
              }          
          }    
       }]);
    })(window.angular);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
      <div ng-app='app' ng-controller="ExampleController">
        <select ng-change='setModel()' ng-model='temp'>
          <option ng-repeat='option in list track by option.name' ng-attr-title='{{option.age}}'>{{option.name}}</option>
        </select>
        <div>something: {{something | json}}</div>
      </div>

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

What is the best method for choosing the parent elements?

I am attempting to create a sidebar that saves the last clicked link in local storage and still displays the collapsed links after the page is reloaded. $(".clickedLink").parent().parent().css('background-color', 'green'); Ca ...

Encountered a problem while trying to upload a file using node

Hi there! I seem to be facing an issue with the code snippet below. Every time I attempt to upload a file, the browser keeps loading indefinitely. Any thoughts on what might be causing this problem? app.js var formidable = require('formidable') ...

Dealing with Cross-Origin Resource Sharing (CORS) issues in AngularJS when using Node, Express, O

Recently, we made the decision to transition our front-end from EJS to Angular and separate the frontend and backend completely. As we started this migration, we encountered various issues across different browsers. Our backend utilizes Node with express, ...

Ordering by the scope function and then by a specific field can be achieved by following these

Is it possible to order by the result of getTotal() and then arrange by quantity in descending order? <!DOCTYPE html> <html ng-app="shop"> <head> <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14 ...

Passing an array from PHP to the DataTables JavaScript library

I am attempting to pass a PHP array to the JS Datatable library for testing purposes. Instead of fetching data from a database, I have simplified my approach. Here is a basic representation of my PHP code: $data_fmt['data'][] = array("TEST"); e ...

`The ng-binding directive seems to be malfunctioning while ng-model is functioning properly

Currently, I am in the process of learning how to utilize Angular (1.3.10). My objective is to create two input fields that will specify the suit and value for a hand of playing cards. In my attempts to achieve this, I have encountered an issue. When I man ...

Utilize a variable function in AngularJS

Within my app.js file, I have declared functions in the following manner: var func1 = function(v1,v2,v3) { . . } var func2 = function(v1,v2,v3) { . . } Moving on to my controller.js file: var action = ""; if(..) { action = 'func1';} ...

Utilizing jQuery to dynamically calculate real-time output from user input in a form

My task involves creating a form where the user fills out certain values, and I want to display a calculated value based on those inputs at the bottom of the form. Unfortunately, I'm not seeing any output at all. Here is the HTML form I have attempte ...

What is the best way to organize this JSON Object based on price using Javascript?

Looking to organize a JSON object by sorting based on price from lowest to highest. { "BEY": { "1": { "price": 2280, "airline": "QR", }, "2": { "price": 2108, "airline": "UA", } }, "BKK": { "1": { "price": 2956, "airli ...

Automatically resizing font to fit the space available

I am attempting to achieve the task described in the title. I have learned that font-size can be set as a percentage. Naturally, I assumed that setting font-size: 100%; would work, but unfortunately it did not. For reference, here is an example: http://js ...

Using Backbone for the front end and Node.js for the backend, this website combines powerful technologies

Currently, I am in the process of developing a new website that will function as a single-page application featuring dialog/modal windows. My intention is to utilize Backbone for the frontend and establish communication with the backend through ajax/webs ...

Is it possible for the React Material-UI InputBase component to trigger an onSubmit action?

Currently, I am working on a React web application using Material UI and trying to implement InputBase for text input with an action triggered by pressing the 'Enter' key. While I have successfully set up the onChange event to update the input, I ...

Determining the Similarity of jQuery Selectors' Selected Elements

I'm looking for a way to programmatically identify if two jQuery selectors have chosen the exact same element. My goal is to iterate over multiple divs and exclude one of them. This is what I envision: var $rows, $row, $row_to_exclude; $rows ...

Challenges with receiving form submissions

I'm new to asking questions here, but I've been reading articles and questions for a while now. My question is about a specific part of a code that has been causing me trouble. In my project, I have a registration form with both JS validation an ...

Enhancing Your Styles with a Second Transition in CSS

I am facing an issue with a button that triggers the appearance of a div from the bottom with a smooth transition lasting 0.5 seconds. However, when the div transitions down to disappear, it snaps down abruptly instead of transitioning smoothly. I have att ...

Using ReactJS to set data values within an array

I just started learning ReactJS and I'm trying to figure out how to read an excel file and display the data in my React project. While I have successfully managed to read the data from the excel file, I am facing issues with displaying it in the react ...

Issue with React-Redux state not updating properly in setInterval()

I am encountering an issue with react-redux / react-toolkit. My state is called todos and it is populated with 3 items correctly, as shown in this image: https://i.sstatic.net/LThi7.png Below is the code of my todo slice: import { createSlice } from &apo ...

Cordova wake-up call device

After successfully creating a Cordova alarm clock app with a timer, I am now looking to add a visual and audio alarm notification for when the alarm goes off. However, I have encountered a problem as the current plugin I used for local notifications (https ...

The 'exhaustive-deps' warning constantly insists on requiring the complete 'props' object instead of accepting individual 'props' methods as dependencies

This particular issue is regarding the eslint-plugin-react-hooks. While working in CodeSanbox with a React Sandbox, I noticed that I can use individual properties of the props object as dependencies for the useEffect hook: For instance, consider Example ...

Confused within the realm of javascript's scope

Hello, I'm looking for some assistance with the code snippet provided below. My goal is to call the function doTheSlide() from within the slide event function in JavaScript. However, I seem to be struggling with understanding scope in JS. Could someo ...