Generating Angular select options dynamically using ng-repeat

I've been struggling for several days now with ngOptions and ngRepeat. I am trying to create a select option for a people control. The REST service is returning an array as shown below. Despite trying various solutions from Stack Overflow and the Angular documentation, I am unable to display even a single attribute. What am I doing wrong?

Below are some of my attempts.

Thank you in advance for any advice and assistance.

var myApp = angular.module('myApp',[]);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {
    $scope.name = 'Superhero';
    $scope.players = [{"id": "1", "nickName" : "Cole", "firstName" : "Mark", "lastName" : "Coleman"}, {"id" : "2", "nickName" : "West", "firstName" : "Peter", "lastName" : "West"}];

}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form>
        <label>Captain:
            <select ng-model="team.captain">
                <option ng-repeat="player in players" value="{{player.id}}"> {{player.firstName}} {{player.lastName}}</option>
            </select>
        </label><br />
    </form>

Answer №1

Don't forget to include your controller in the application:

angular.module('myApp').controller('MyCtrl', MyCtrl);

Next, make sure to add

<html ng-app="myApp"></html>
in your HTML template.

Finally, specify the controller in the template to manage the desired section like this:

<form ng-controller='MyCtrl'>

Answer №2

<!DOCTYPE html>
<htm ng-app="myApp">


<body>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form ng-controller="MyCtrl">
..            <select ng-model="team.captain">
                <option ng-repeat="player in players" value="{{player.id}}"> {{player.firstName}} {{player.lastName}}</option>
            </select>
     </form>
  <script>
    var myApp = angular.module('myApp',[]);

 function MyCtrl($scope) {
    $scope.name = 'Superhero';
    $scope.players = [{"id": "1", "nickName" : "Cole", "firstName" : "Mark", "lastName" : "Coleman"}, {"id" : "2", "nickName" : "West", "firstName" : "Peter", "lastName" : "West"}];

}

myApp.controller('MyCtrl',MyCtrl);
    </script>
</body>

</html>

Answer №3

To properly set up the controller for the myApp module, you need to follow these steps:

First, register the controller like this:

var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', MyCtrl);

function MyCtrl($scope) {
    $scope.name = 'Superhero';
    $scope.players = [{"id": "1", "nickName" : "Cole", "firstName" : "Mark", "lastName" : "Coleman"}, {"id" : "2", "nickName" : "West", "firstName" : "Peter", "lastName" : "West"}];
}

Next, in your HTML code, make sure to include ng-app="myApp" and ng-controller="myCtrl" like this:

<html ng-app="myApp">
  <head>
    // Include your scripts here
  </head>
  <body ng-controller="myCtrl">
    <form>
        <label>Captain:
            <select ng-model="team.captain">
              <option value="">Select one</option>
              <option ng-repeat="player in players" value="{{player.id}}"> {{player.firstName}} {{player.lastName}}</option>
            </select>
        </label><br />
    </form>
  </body>
</html>

For a demo, you can check out this PLUNKER DEMO LINK

Answer №4

Hello, Give this a shot :

APP.JS CODE

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.name = 'Hero';
    $scope.players = [{"id": "1", "nickName" : "Cole", "firstName" : "Mark", "lastName" : "Coleman"}, {"id" : "2", "nickName" : "West", "firstName" : "Peter", "lastName" : "West"}];
    $scope.selected = $scope.players[0];
}

HTML CODE

<select ng-options="player as player.nickName for player in players ng-model="selected"></select>
  1. Check it out with this live demo : Select in AngularJs Fiddle

Thank you & Best wishes

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

404 Error: JSON POST and GET Request Not Located

I need assistance with setting up an API in Django as I am encountering errors in the JavaScript console. The error messages are: GET http://127.0.0.1:8000/edit/undefined 404 (Not Found) POST http://127.0.0.1:8000/edit/undefined 404 (Not Found) Is there a ...

Tips for displaying "in progress" in AngularJS when data loading is slow

I am experiencing sluggish loading times with my ng-repeat function in AngularJS. This is causing my browser to freeze up. How can I implement a loading animation or indicator to show the user that the data is still being loaded? ...

Utilizing AngularJS: Implementing a directive to invoke a controller function with parameters

One concept I am already familiar with involves calling a controller function without arguments inside a directive. The following code demonstrates this: Firstly, in the directive definition: scope: { actionFunc: "&" } Within the directive template: ...

Creating HTML tabs using jQuery with input[type="radio"] tutorial for beginners

I'm having trouble creating a tab with input[type="radio"] using Jquery. The tab works fine on the first click, but it doesn't work on the second click. I can't figure out where the issue is in the Jquery code. Can someone assist m ...

What is the best way to convert my Chatbot component into a <script> tag for seamless integration into any website using React.js?

I have successfully integrated a Chatbot component into my Next.js application. https://i.stack.imgur.com/BxgWV.png Now, I want to make this component available for anyone to use on their own website by simply adding a tag. My initial approach was to cre ...

Prefering `window.jQuery` over the yarn version

I am currently in the process of transitioning to Vite 3 with Vite Ruby on Rails from Webpacker and Webpack. One major issue I have encountered is that my system functions as a CMS. Some of our long-standing clients have jQuery code embedded directly withi ...

Cease the use of jQuery animations

My JavaScript code snippet looks like this: $.get("/<page>.php", "userid='.$userid.'&"+status, function(data){ $("#status").show("fast").html(data).delay(4000).hide("fast"); }); On a page with multiple links triggering thi ...

Storing an array in $cacheFactory with AngularJS

Having some trouble saving an array in AngularJS' $cacheFactory. When attempting to retrieve the array, it's coming back as undefined. Here is the code snippet: angular.module('cacheExampleApp', []). controller('CacheContro ...

What is the process for developing a personalized search feature in VueJS?

How can I create a custom search function in VueJS that allows me to input partial product names and receive relevant results? For example, I have 2 products named PlayStation Plus 90 Days IE and PlayStation Plus 90 Days NO. Currently, I have to input the ...

Ways to dynamically manipulate HTML elements in Angular 5

Recently, I've been attempting to programmatically transform an HTML element. Strangely, when I update the transform value in the console tab, it changes successfully, but for some reason it doesn't reflect in the element tab of the browser. onD ...

Challenges with using async await alongside synchronous functions

I'm currently navigating through a library that utilizes async functions and feeling a bit overwhelmed. I'm attempting to call a function that should return a string, but I'm hitting some roadblocks. As I understand it, the ZeroEx library fu ...

The download functionality in HTML5 is not functioning properly, so users are forced to rename files when downloading

For weeks, I've been struggling to change the name of the downloaded file. Despite my efforts, instead of being named Chrysanthemum.jpg, it ends up as a hash of the file like 241693260.jpg In my backend setup, I utilize Node.js and Express.js for man ...

Include a return or delete from the default IO statement

I am utilizing an intersection observer that alters the header's font-color and background-color based on the content intersecting with it. This change is determined by the presence of data-color and data-background attributes declared on the div/sect ...

Adjust the size of the font for the placeholder text

I've been attempting to adjust the font size of the placeholder text. I added the font size property to the listed classes below, but for some reason, it's not taking effect. Could you please advise me on how to resolve this issue so that I can ...

Discovering the selected row with jqueryIs there a way to locate

There is a table with rows and a button that allows you to select a row: <table id="mytable" class="table-striped"> <tbody> <tr id="1"><td>Test1</td></tr> <tr id="2"><td>Test2</td>& ...

When utilizing Vue components, the issue of "setattr" arises when trying to assign

Having recently started using Vue.js, I encountered an issue while trying to implement components. Interestingly, the code worked perfectly fine before implementing components. I'm facing an error that is proving to be quite challenging to understand ...

How to incorporate markdown files as strings in Next.js

Is there a way to bring in markdown files as strings in Next.js for use on both the client and server sides? ...

The functionality of the Bootstrap carousel for moving to the next and previous images is malfunctioning, as it only

The carousel on my website is not functioning properly. It only displays the first image and does not slide to the next pictures as it should. Here is the code snippet for the carousel: <body> </nav> <div id="carousel1" class="carousel slid ...

Having difficulty accessing information from a webservice on the Ionic1 framework

I am a beginner with the ionic1 framework and currently working on a sidemenu ionic app. My goal is to fetch a list of playlists from a Mysql Database. I attempted to do this in my controller.js : .controller('PlaylistsCtrl', function($scope,$ ...

Intersection observer automatically removes images from carousel (Siema) after they have been viewed

Check out this example to see the issue I'm facing. I've implemented an intersection observer for lazy loading images, here's the code: const pictures = document.querySelectorAll("[data-src]"); function loadPicture(pic){ const src = p ...