Issue with ng-selected when used alongside ng-options or ng-repeat in Angular

My application features a form where users can fill out their starting point and choose from 350 possible destinations to get directions using Google Maps. Users can select their destination by either clicking on a pin on the map or choosing from a drop-down menu in the form.

Ideally, when a user clicks on a pin on the map, I want the selection in the drop-down menu to change to reflect their chosen destination.

I've attempted to use ng-selected for this functionality, but so far it hasn't been successful.

Any assistance with solving this issue would be greatly appreciated. Thank you.

View

   <div id="select_destination">
        <select data-ng-model="selectedDestination" 
                ng-options="station.stationID as station.stationShortAddress for station in allStationsMapData track by station.stationID" 
                data-ng-change="selectDestination(selectedDestination)"
                ng-selected="station.stationID == selectedDestination">
        </select>
    </div>

Controller

A function called getDirections is triggered when a user clicks on a station on the map (instead of selecting from the drop-down menu). This function is intended to update the currently selected item in the drop-down menu, but it's not working as expected. (Note: The function is correctly passing in the stationID.)

$scope.getDirections = function(stationLat, stationLng, stationID){ 
    //update the select menu model to the chosen stationID.
    $scope.selectedDestination = stationID;     
};

Extra info:

I also attempted using ng-selected within ng-repeat, but that approach did not yield the desired result either.

<select data-ng-model="selectedDestination" data-ng-change="selectDestination(selectedDestination)">
    <option ng-repeat="stationMapData in allStationsMapData" 
        value="{{stationMapData.stationID}}" 
        ng-selected="{{stationMapData.stationID == selectedDestination}}">
        {{stationMapData.stationShortAddress}}
    </option>
</select>

Answer №1

modify the

ng-selected="station.stationID == selectedDestination"
to
ng-selected="selectedDestination"
.

Alternatively, you can utilize ng-init instead of ng-selected. (It provides a clearer way.)

ng-init="selectedDestination = 'key of what you want'"

Check out the Fiddles for reference.

FIDDLE(First situation)
FIDDLE(Second scenario)

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 can I make a clickable button or link within an anchor tag that is still able to be right-clicked?

Hey there, I'm currently working on a notification dropdown menu where each <li> contains a link or an <a> tag. I'm aiming to create something similar to Facebook's notification system. However, the challenge lies in trying to ...

Can you provide a list of factors that influence coverage? Additionally, is there a specific algorithm available for calculating

As part of my Angular project, I am currently focusing on writing unit test cases using the Jasmine Framework and Karma. To ensure thorough testing coverage, I am utilizing Coverage-html (Istanbul). When it comes to coverage, there are several important t ...

Angular event triggered when updating input values from the model

I have developed a custom directive to add functionality to input fields with a specific class. I want to trigger events on blur and focus to update the label style based on Material Design principles. However, when using ng-model in Angular, I also need t ...

Implementing class changes based on scroll events in JavaScript

const scrollList = document.getElementsByClassName('scrollList'); function scrollLeft() { scrollList.scrollLeft -= 50 } function scrollRight() { scrollList.scrollLeft += 50 } #scrollList { display: flex; overflow: auto; width: 10 ...

Notification from Apple - one message, multiple alerts received

After sending a notification to an iOS device, only one message is received, but the alert pops up multiple times (iOS 7+). However, when the notification is pushed to an Android device, everything works as expected: one message received and one alert disp ...

What improvements can I implement to enhance the clarity and functionality of this form handler?

As a beginner working on my first Next.js app, I'm facing some challenges that seem more React-related. My app allows users to add and edit stored food items and recipes, requiring me to use multiple submit form handlers. Each handler involves: Che ...

Challenges with implementing speech recognition within a React component's state

I've encountered an issue with the React library react-speech-recognition. When trying to modify the newContent state within useEffect, it ends up printing as undefined. Additionally, I'm facing a similar problem when attempting to update the sta ...

The code "Grunt server" was not recognized as a valid command in the

I recently set up Grunt in my project directory with the following command: npm install grunt However, when I tried to run Grunt server in my project directory, it returned a "command not found" error. Raj$ grunt server -bash: grunt: command not found ...

Implementing a JavaScript function to a button within an existing form in JSP - Best practices

Currently, I am working on developing multiple JSP pages and I encountered a requirement where I needed to add a function to a button within an already existing form. The challenge was that the form's submit button was configured to navigate to anothe ...

What is the method for sending arguments to material avatar using require?

import Avatar from '@material-ui/core/Avatar'; Here is an example of working code: <Avatar alt="user 4" src={require('Assets/img/user-1.jpg')} className="size-80 rounded-circle border-info rct-notify" /> However, I encountered ...

Utilize JavaScript objects to convert subscripts into HTML elements

I am currently developing an Angular 1X application where all icon labels are stored in a JS object as shown below: var labels={ ........ iconlabel_o2:"O<sub>2</sub>", iconLabel_co2:"CO<sub>2</sub>", iconLabel_h20:"H<sub ...

Creating content by extracting key value pairs from a JSON object retrieved from Firebase

After setting up a Firebase database and populating it with JSON data, I found code from the Firebase documentation that allows me to update the data in real time. While this works well for my needs, I also want to access the values in the JSON to create l ...

What is the procedure for canceling a readline interface inquiry?

TL;DR I encountered an issue with the native Node.js Readline module. Once a question is asked using rl.question(query[, options], callback), it seems there is no straightforward way to cancel the question if it's pending an answer. Is there a clean ...

Converting Node.js Date.toString() output into a time format in Go

My go service is currently receiving data from an external source. Here's how the data appears (in JSON format)- { "firstName": "XYZ", "lastName": "ABC", "createdAtTimestamp": "Mon Nov 21 2 ...

Tips for preparing HTML content with Jquery's "ON" method?

My jQuery Accordion is functioning properly, but I'm encountering issues when trying to load the accordion content dynamically from a database or JSON onto the page. The problem arises because the DOM doesn't have information about the newly inje ...

Using JavaScript to retrieve and compare element values for a total sum

Given two arrays, the goal is to determine if any two numbers in the array add up to 9. The function should return either true or false. For example: array1 [1, 2, 4, 9] has no pair that sums up to 9, so it returns false array2 [1, 2, 4, 5] does have a ...

Encountering issues with browser tabs and Socket.IO

I'm currently working on a real-time chat using Socket.IO, but I've encountered a major issue. The aim is to allow users to log in, select another connected user, and start chatting... var http = require('http'), fs = require(&ap ...

Retrieving Mouse Coordinates using Ajax in PHP

I'm wondering if it's feasible to send an Ajax request with mouse coordinates using PHP. For instance, I am fetching a page with cUrl and would like to trigger a mouse movement event on that page. At this point, I haven't written any code ...

Creating mathematical formulas in JavaScript

How can the equation be translated into JavaScript? This is the proposed programming solution, but there seems to be an issue. var MIR = parseFloat(($('#aprrate').val() / 100) / 12); var paymentAmount = (MIR * $('#amounttofinance').va ...

Stop the HTML5 video playback within a slider (flickity)

As I add slides to a slider using flickity, I am encountering an issue where the first video pauses when there is a slide change event. However, if I play the video on the next slide and then move back or forward, the video does not pause. This is my curr ...