angular ignoring selected option

Upon examining the HTML code below, there is an issue with selecting an option based on the sport.slug:

<div class="form-group" ng-hide="controller.sports.loading">
    <label for="sport">Sport</label>
    <div class="input-group">
        <div class="input-group-addon">2</div>
        <select class="form-control" id="sport" ng-model="controller.team.data.sport">
            <option ng-repeat="sport in controller.sports.data" value="{{ sport.slug }}">
                {{ sport.title }}
            </option>
        </select>
    </div>
</div>

The selected option does not reflect the sport.slug in the model. The ng-repeat seems to be causing this mismatch between the value and text displayed.

To align the selection with the sport.slug, adjustments need to be made. Here's a snapshot of the model for reference:

var team = {
    loading: false,
    data: {
        id: 1,
        name: 'Test',
        sport: 'tennis'
    }
};

Answer №1

I recommend utilizing the ng-options directive

<select ng-model="controller.sport.data.type" ng-options="type.value as type.label for type in controller.types.data">
</select>

CORRECTION: I mistakenly switched the order of type.value and type.label initially

Answer №2

Give this a shot -

<select ng-model="viewModel.selectedSport" ng-options="sport.id as sport.name for sport in viewModel.sportsList"></select>

Always strive to leverage the power of Angular features whenever possible.

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

Personalized customizable elements in Draft.js (extension)

For instance, I am interested in enabling users to incorporate a dynamic slideshow of images into their page using Draft.js. This would allow them to select a predefined slideshow from my app and have it automatically update on their page if the images a ...

Unraveling the Mystery of jQuery AJAX Response: Where Have I Gone Astray?

$.ajax({ type: 'POST', url: 'place/add', data: { lat: 45.6789, lng: -123.4567, name: "New Place", address: "123 Main St", phone: "555-1234", review: "Great place!", cat ...

nodejs promises and their implementation in loops

Currently delving into the realm of promises in nodejs, here's an example code snippet for you to peruse: The result when running the below code is as follows: test - 1 test - 2 test - 3 test - 4 var Q = require('q'); var promise = Q.when( ...

The best way to avoid routing before certain async data in the Vuex store has finished loading

I am facing a challenge in my application where I require certain data to be loaded into the VueX store before routing can commence, such as user sessions. An example scenario that showcases a race condition is as follows: // Defined routes { name: &ap ...

Unable to pass parameter in ngRoute

When I pass /?preview=true in my function and catch it in app.js with $route.current.params.preview, it appears to be undefined when outputting to the console log. Can someone please assist me in resolving this error? Below is blah.js which will navigate ...

Include onload to element without running it in Puppeteer

I am currently developing a web scraping tool using Puppeteer. Once the webpage is fully loaded, I need to update the HTML code and include an onload event for certain elements. The issue is that in Puppeteer, the onload event is actually triggered automa ...

Retrieving the user's Windows username with JavaScript

Is it possible to retrieve the Windows user name solely in Internet Explorer using the code below? function GetUserName() { var wshell = new ActiveXObject("WScript.Shell"); alert(wshell.ExpandEnvironmentStrings("%USERNAME%")); } What methods ...

The ReactDom reference is not defined when working with React and webpack

After following a tutorial on React and webpack from jslog.com, I encountered issues with using updated syntax such as using ReactDom to call render instead of the deprecated 'React.renderComponent'. I tried running: npm install --save react-do ...

Steps to update the background color of a highlighted ItemList component in Material-Ui

I recently implemented a selectable component using Material-UI let SelectableInfiniteList = makeSelectable(Infinite); and added ListItems inside. Now, I am trying to customize the default gray color of a selected item. I attempted to do this using a cla ...

assigning attributes to web addresses

Is there a way to set a style property for webpages by targeting addresses that contain /index.php/projecten/ instead of specifying each complete address in the code? Currently, I am using the following code: <ul class="subnavlist" style="display: &l ...

Retrieve the callback arguments using sinon.spy within a JavaScript promise

During my test with mocha and sinon, I encountered an issue where I couldn't retrieve a callback value from inside a promise scope of an HTTP-request due to the asynchronous nature of promises. It seems that by the time sinon.spy checks on the callbac ...

Guide to updating the value of a CSS seconds dynamically

let timerSeconds = 10; // This value will be retrieved from a database function StartCountDownTimer() { let timerStart = new Date(); let css_seconds = `${timerSeconds}s`; const css_root = document.querySelector(':root'); let cssVa ...

AngularJS and ASP Controller API encountering CORS problem while being hosted on separate servers within a local network

I am facing an issue with my ASP application hosted on a server within a local network. Whenever I make an OPTION request from AngularJS, it returns a 200 status but throws an error. Below is the GET code in Angular: var config = { headers: { ...

Optimizing jQuery Dialog for Different Window Sizes

I am currently developing a responsive website and facing a challenge. I need to implement a popup for window sizes smaller than 480px, but on desktop screens, the content should be visible without being inside the popup. I want to avoid duplicating the co ...

Bootstrap side navigation bars are an essential tool for creating

Is there a way to change the hamburger icon in Bootstrap's navbar-toggle to an X button? I want the side nav to slide to the left when clicked, and also collapse when clicking inside the red rectangle. Thank you. https://i.sstatic.net/nQMGs.png Code ...

unable to achieve two-way binding with ng-model in textarea

I'm encountering a strange problem with AngularJS. I have a textarea with a default value, but when I manually change the value in the textarea, it isn't being updated in my controller. Additionally, the other scope is not binding to the default ...

When $save in AngularJS is not functioning properly, make sure to trigger a callback

I have a save method in my code snippet. $scope.save = function(){ $scope.product.$save(function(pipe){ alert("fire!"); //not showing }); }; The save method is working as intended and successfully saving the post to the database. Howev ...

Tips for adding a dynamic variable into a react JSX map function

I have a component that receives a props with the value of either 'A', 'B', or 'C' based on the selection made in the parent element. The challenge is to use this props to dynamically select an option list locally, instead of ...

EaselJS: Enhancing Performance and Aesthetics with Over 200 Vector Shapes

When comparing EaselJS performance with Canvas native methods, I noticed a significant difference: 2.2 s vs 0.01 s (despite EaselJS mapping canvas native methods). I created a canvas application that draws a tree*. For animating the growth of the tree, us ...

Using the .load() function to import an HTML file from the directory above

I am trying to achieve the following structure: folder1 index folder2 page to load Can I dynamically load the 'page to load' in a div within the 'index' page using DIV.load()? I have attempted various paths (../folder2/page, ./, ...