AngularJS - retrieving and displaying the selected value from an HTML dropdown menu

Could someone help me figure out why the Land selection is empty when trying to display it as {{ selectCenter.land }}?

For reference, here is a functional plunker:

http://plnkr.co/edit/Q8jhdJltlh14oBBLeHJ9?p=preview

And the relevant code snippet:


    <!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <link rel="stylesheet" href="style.css">
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <h1>ngOption demo</h1>
    <div ng-controller="SelectCtrl">
    Search: <input ng-model="searchText" type="text">
    <select
      ng-model="selectCenter"
      ng-options="center.land for center in centre | filter:searchText"
      multiple class="form-control input-lg">
    </select>
    You have chosen:
    {{ selectCenter }}
    </div>
  </body>

</html>


    // Code goes here
angular.module("myApp", []);

angular.module("myApp").controller("SelectCtrl", ["$scope", function ($scope) {
  $scope.centre = [
    {"id":"122","shopID":"200","land":"denmark"},
    {"id":"123","shopID":"201","land":"france"},
    {"id":"124","shopID":"202","land":"italy"}
  ];
}]);

Answer №1

When utilizing multiselect, your model selectCenter becomes an array rather than an object, which affects how it is displayed.

{{selectCenter.land }}

However, you can create a repeater like this:

<span ng-repeat="center in selectCenter">{{center.land}}</span>

To see a working demo, visit the following link: http://plnkr.co/edit/CXk6ag3XK85JSoWKJYcM?p=preview

Answer №2

eliminate multiselect option

<select ng-model="selectCenter" 
      ng-options="center.land for center in centre | filter:searchText"
   class="form-control input-lg">

to see the updated code, click on the following link

http://plnkr.co/edit/NUp6OuFty4lbAd4mwn6D?p=preview

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

The issue of the Angular controller not functioning properly in conjunction with Node Express

After creating an angular app (version 1.5.7), I wanted to deploy it to heroku. To do this, I implemented Node and used express to serve the main index.html file for heroku to build since it doesn't support plain angular apps. However, after making th ...

Shifting divs to different positions upon clicking

I am currently working on a project where I have three divs in a container positioned next to each other. My goal is to make them change their positions when clicked. For example, clicking on the left div should move it to the center position. Here is my p ...

When I delete the initial element from the array, the thumbnail image disappears

Using react-dropzone, I am attempting to implement image drag and drop functionality. The dropped image is stored in the React state within a files array. However, a problem arises when removing an image from the array causing the thumbnails of the remain ...

Adding descriptive text before a website link is a helpful way to provide context for the reader. For example

My goal is not just to have JavaScript change the URL after my page has loaded. I want to be able to enter something like 'blog.mywebsite.com' into the omnibar and have it locate my website similar to how Steam does with 'store.steampowered. ...

Angular2's $compile directive functions similarly to AngularJS 1's $compile directive

I am currently in the process of migrating my project from Angular 1 to Angular 2 and I am in need of a compile directive. However, I am struggling to rewrite it to achieve the same functionality as before. app.directive("compile", compile); compile.$inje ...

What is the best way to prevent two paths within an SVG from intersecting with each other?

Currently, I am developing a program that allows the user to draw lines. Once the user completes drawing a line, I receive an array of points in this format: [[x, y], [x, y], ...]. I then convert these points into a path string using the following functio ...

From AJAX response to React state attribute, store the JSON data

I have a component where I need to fetch freight values via ajax and store them in the state property of this class. import React, { Component } from 'react'; import Freight from './Freight'; import CreateFreightEntryModal from '. ...

An error of type 'TypeError' has occurred, where it is unable to access the property 'render' of an undefined element in

I'm using a function in my controller to render the "result" in my "home-page" view, which is calling my model to execute the query. exports.searchNoms = (req, res) => { getDatabaseModel.searchNoms(req).then(function(result) { console.l ...

What is the best way to partition JSON data from an API request in React and display it in various sections within the component

There are 2 JSON objects that contain sales period details based on Month to date and year to date. The data includes information such as Units Sold, Gross Revenue, Year to Date Totals, Month to Date Averages, Expenses, Net Revenues, and Per Unit values. I ...

Managing errors that occur while handling JSON with AJAX by implementing a suitable backup plan

Imagine there is a user.json file stored on a web server that contains: { "name” : “ivana”, “age” : “27” } Now, I make a request to retrieve this JSON data using the following code: var user = $.ajax({ url: pathsample.com/user.js ...

What is the best way to create an animated, step-by-step drawing of an SVG path

I am interested in creating an animated progressive drawing of a line using CSS with SVG/Canvas and JS. You can view the specific line I want to draw here <svg width="640" height="480" xmlns="http://www.w3.org/2000/svg"> <!-- Created with cust ...

What are the benefits of having a dedicated REST backend API?

Background: I am a novice programmer who is self-taught in the hopes of creating a Single Page Application. I have started learning JavaScript, jQuery, PHP, and MySQL, and now feel quite comfortable with all of them. I began working with Ember, but now I a ...

Vue Material - Effortlessly integrate native transitions within router views

In the project I'm currently working on, I have chosen to use Vue Material for the development of a single-page application. The approach I am taking follows a common trend in which a central "container" component is utilized to manage the shifting vi ...

Using the array.prototype.map method on props in React.js results in an array that is devoid

Recently, I've started exploring the world of React and encountered a problem while attempting to convert the value of props into a JSX element using array.prototype.map(). You can learn more about this method at this link. Here is a snippet of a Rea ...

What is the best way to integrate a React component into an Angular project?

Struggling with integrating a React component into an Angular project and unable to make it work. I have a JavaScript file containing the React component that I want to use in Angular. Here's an example: React file... import React from "react"; c ...

Issues with AJAX formData functionality

I'm having difficulties with the formData in my Ajax calls. I have searched extensively for solutions and tried various approaches, including using getElementById, but nothing seems to work. The form in question has an id of add-lang-form: <form ...

How to send a scope variable as part of a string to an AngularJS directive

I'm new to AngularJS Directives and would like to know how I can pass a custom string, which is a URL containing a controller variable, for later use in that controller? Calling Directive <div ng-controller="MyController"> <my-custom-fo ...

A different approach to fixing the error "Uncaught (in promise) TypeError: fs.writeFile is not a function" in TensorFlow.js when running on Chrome

I've been attempting to export a variable in the TensorFlow posenet model while it's running in the Chrome browser using the code snippet below. After going through various discussions, I discovered that exporting a variable with fswritefile in t ...

Is it possible to utilize the expose method to retrieve additional reactive variables and computed properties similar to methods in Vue 3?

Switching my application from Vue 2 to Vue 3 has been quite a journey. I've utilized the Composition API to refactor my previous render function into the setup hook. One interesting discovery was the ability to expose methods using context.expose({}). ...

"Engaging with the touchscreen inhibits the triggering of click

Within this div, I have implemented touch-action:pan-y;. Surrounding this div is an anchor tag. If you click on the div, the link will successfully redirect. However, if you swipe on the div and then click, the link won't work on the first attempt bu ...