The ng-repeat directive successfully renders elements such as <p>, <ul><li>, and more, but encounters issues when used with a select element that has the same data source

A component of an application that generates a list of popular phone and tablet models based on the selection of a mobile operating system is displayed below:

<div class="input-field col s3">
                <label class="active">Environment (OS)</label>
                <select ng-model="environment" ng-change="listBrowsers()">
                    <option value="" disabled>Select an OS</option>
                    <option ng-repeat="OS in OSes">{{OS}}</option>
                </select>
            </div>

Here is an excerpt from the $scope.listBrowsers() function in the controller for context....

$scope.listBrowsers = function() {
    $scope.browsers = MachineDataService.getBrowsers($scope.environment);
    $scope.browser1 = null;
    $scope.notMobile(); //calls another function to determine mobile-ness
    $scope.getMobileDevices($scope.environment);
    return $scope.browsers;
}

Afterwards, the 'getMobileDevices' scope function invokes a corresponding service method:

this.getMobileDevices = function(envt) {
    var deviceList = [];
    for (i=0; i < _devices.length; i++) {
        if (_devices[i].versions.indexOf(envt) > -1) {
            deviceList.push(_devices[i].name);
        }
    }
    return deviceList;

}

However, a problem arises when making changes to the view:

This approach works --

<div class="input-field col s6">
                <label class="active">Phone/Tablet Model:</label>
                <p ng-repeat="device in mobileDevices">{{device}}</p></div>

This approach doesn't:

<div class="input-field col s6><select ng-model="mobileDeviceType">
                    <option value="" disabled selected>Choose a device...</option>
                    <option ng-repeat="device in mobileDevices" value="{{device}}">{{device}}</option>
                </select></div>

Interestingly, even substituting the select with radio buttons worked as expected!

Disclaimer: It's possible I may be overlooking something -- Angular is still relatively new to me. Apologies for not utilizing Angular Material initially; I plan to switch over later.

Edit -- To further illustrate the issue, here are screenshots showcasing the result with the paragraph using the repeater, versus the select element implementing ng-options.

sample-with-ngoptions sample-with-paragraph-ngrepeat

Answer №1

Surprisingly, the code worked perfectly for me! I used the snippet that you provided to create a demo, and it's functioning well. Have you double-checked to ensure you are following the same steps?

However, there is a syntax error in the code snippet you shared:

<div class="input-field col s6><select ng-model="mobileDeviceType">

Please correct it to

<div class="input-field col s6"><select ng-model="mobileDeviceType">
. You missed adding a closing " at the end of the class attribute.

var adminPage = angular.module("adminPage", []);
adminPage.controller('adminPage1', function($scope, $http, $window) {

  $scope.mobileDevices = ['Apple', 'HTC', 'Samnsung', 'One plus'];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="adminPage" ng-controller="adminPage1" class="input-field col s6">
  <select ng-model="mobileDeviceType">
    <option value="" disabled selected>Choose a device...</option>
    <option ng-repeat="device in mobileDevices" value="{{device}}">{{device}}</option>
  </select>
</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

How to host two Node.js socket.io projects on one URL

My Nodejs server is hosted on Amazon Lightsail. I have successfully connected to it and configured Apache for Nodejs. I am working with 2 Nodejs socket.io projects and I want to access both of them using a single URL. Below are the links in Nextjs: const ...

Selection dropdown not being populated by ng-options

After trying numerous examples to implement the changes, I am still facing issues. Even though I have an ng-model and clearly defined the object property I want to receive, it is not functioning as expected. Any assistance would be appreciated. Here is th ...

A guide to setting up a unit test for an Angular JS controller that utilizes an ajax service

I've been attempting to write unit tests for the code below, but I'm struggling to make it work. Service Code: angular.module('ActivityApp').service('PersonService', [ '$http', function ($http) { v ...

Activate the 'li' element within the 'ul' whenever a 'ui-sref' link on the

I have been working on building a dashboard that consists of an abstract state called dashboard, with subdashboard as the immediate child state. https://i.sstatic.net/YTHky.png Previously, clicking on user details would take the user to the dashboard.tab ...

Utilize jQuery append() to create a grid layout using <td> tags

I need to integrate address information from an object into an HTML table using the append() method. The initial HTML table structure is as follows: <table class="shipping_information" border="1"> </table> To achieve this, I place the addre ...

A beginner's guide to checking the functionality of individual Vue components

I'm looking to implement ava for conducting unit tests on my Vue components. Currently, I have a basic setup in place: package.json { "name": "vue-testing", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { ...

Despite the presence of data, MongoDB is not returning any values

I am currently working on a code that utilizes $geoNear to find the closest transit stop to a given GPS coordinate. The issue I am facing is that the result sometimes returns undefined, even though I have confirmed that the data exists in the STOPS collect ...

What could be causing JavaScript to fail in recognizing my constructor?

I encountered a problem where the script fails to recognize the constructor in the class and incorrectly assumes that I am calling all the other functions as constructors. class Articles { constructor(dbName = ':memory:') { return(asy ...

Can dynamic checkboxes be utilized for filtering data in a table using Angular 7?

I need assistance with dynamically filtering data in a table using checkboxes. The aim is to allow users to toggle multiple checkboxes to refine the displayed data. Currently, I have achieved some functionality but I am struggling to determine whether or ...

The array of markers in the Google Maps API 3 has not been defined

Trying to manipulate markers on a Google map using data from an XML file is proving challenging. I aim to create and store the markers in an array. markerArray[fzg] = marker; However, when attempting to update the positions of these markers based on new ...

Checking for null values using the "and/or" syntax

While reading an article on special events in jQuery, I came across a syntax that was unfamiliar to me: var threshold = data && data.threshold || 1 I have previously encountered the following: var threshold = data.threshold || 1 As far as I kno ...

ReactJS - What is the best way to output a string from a functional component?

Utilizing @apollo/client in my React project for handling backend APIs. Within the file appollo.js, I am attempting to make a call to the backend API link based on certain conditions. Currently, appollo.js consists solely of functions and is not considere ...

Copy the style of a chosen element and apply it to another element

One of the challenges I'm facing involves a dynamic span element that changes based on color selection: <span class="color checked" style="background-color: #ff0000">Red</span> In addition, I have an image element wit ...

How to use node.js to add JSON data to a JSON file without using an array?

I'm trying to update a JSON file without using an array with FS. The desired format of the file should be: { "roll.705479898579337276.welcomemessage": "There is a welcome message here", "roll.726740361279438902.welcome ...

Is it possible to perform a GET request between a site using HTTPS and another using HTTP?

https://i.stack.imgur.com/AlWYJ.png I recently hosted my site on Shopify using HTTPS, and then I attempted to make a GET request via Angular to a site that uses HTTP. Angular <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.j ...

Use a try/catch block to handle errors in case the parameter does not exist

Sorting the data array is working fine for me, but now I want to pass values as parameters. However, if the user enters a 'parameter' that doesn't exist, I need to display an error message using TRY/CATCH. Can someone please help me understa ...

Updating the background color using typescript

Before transitioning to angular development, I had experience working with vanilla Javascript. I encountered a challenge when trying to modify the css properties of specific elements using Typescript. Unfortunately, the traditional approach used in Javascr ...

AngularJS ngResource failing to pass custom headers

Using ngResource to query a REST API, I am facing an issue with setting my API key in a custom header. Here is how I have attempted to do it: angular.module('ApiService', ['ngResource']) .factory('Api', ['$resource&ap ...

Node responds with a 404 error upon receiving the post request

I am facing an issue with my jQuery code. Here is what I have: $.ajax( { type: 'POST', data : mydata, url : '/routerfunction', dataType : 'String', success : function(data) ...

"Troubleshooting a malfunctioning RewriteBase in .htaccess for a development

Here is the content of my .htaccess file: RewriteEngine On RewriteBase /myapp_dev/ RewriteCond %{REQUEST_URI} /index\.html$ [NC,OR] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] Rewrit ...