"Unleashing the power of ng-options in AngularJS: a guide to dynamically retrieving the index

I am currently working with a select element, where I need both the selected value and dynamically generated index (I do not want to set the default index to [0]). How can this be achieved?

        <div class="input-label">
          Select Mobile
        </div>

        <select ng-model="mobile" ng-options="mobile as mobile.value for mobile in editContactModalData.phoneNumbers"></select>
      </label>

    The code above represents a select option, while below there is an input box where the selected value is displayed.       

      <label class="item item-input item-stacked-label" ng-show="mobile">
        <input type="tel" maxlength="10" placeholder="mobile" ng-model="mobile.value">
      </label>

This pertains to one modal, but I wish to display these changes of the selected mobile in another modal.

The second modal includes:

  <span>{{contact.phoneNumbers[0].value}}</span>

Therefore, instead of specifying the [0] index, I would like to dynamically insert the selected value.

Answer №1

@Nikhil: One important point to note is that ngOptions does not have built-in support for $index like ng-repeat.

If you require the index of a specific value, you can achieve it by using the following approach:

<select 
  ng-model="selectedItem"
  ng-options="values.indexOf(selectedItem) as selectedItem for 
selectedItem in values">
</select>

Answer №2

To change the contact value using ng-change, you can do the following:

<select ng-change="updateContactNumber(phone)" ng-model="phone" ng-options="phone as phone.number for phone in contactPhoneNumbers"></select>

In your controller, define the update function like this:

$scope.updateContactNumber = function(number){

$scope.contactNumber = number.number;

}

And then display the updated contact number in your view like this:

{{contactNumber}}

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

Converting HTML and CSS into a PDF using JavaScript

I am on the lookout for libraries that can cater to my specific needs, as I haven't found one that fits perfectly yet. My setup involves Node.js on Express.js for the backend, and html/css/js for the front-end. Browser support includes IE8 and up, chr ...

Showcase your skills in CSS, HTML, and Javascript

I attempted to search for something similar but came up empty-handed. I have two buttons. When I click one of them, I want to hide a certain division, but it's not working as expected. The divs d2 and d3 are initially hidden when the page opens, whic ...

The functionality of $watch is not functioning correctly within the Modal, despite implementing it with $scope.user={pwd1:''}

For the Plunker link referenced, please click here: http://plnkr.co/edit/0vOjw0?p=preview index.html <!DOCTYPE html> <html ng-app="app"> <head> <link rel="stylesheet" href="style.css"> ...

Would combining node.js with express for the back-end and emberjs for the client side be a suitable choice for my project?

As a seasoned web developer, I have limited experience with nodejs and have yet to dive into the world of emberjs (although I have worked extensively with backbone). I am embarking on a new project to create a web-based writing application focused on lite ...

Sharing Data Across Multiple Windows in Angular 2 Using a Singleton List

My multiplayer game involves adding new players to a single game room lobby, which is essentially a list of current players. How can I update this list for all connected players when new ones join? I implemented a service and included it in the providers ...

How does BullMQ stand out from other message queue implementations?

Recently, I've been exploring the documentation for BullMQ: https://github.com/taskforcesh/bullmq One thing that caught my eye in its comparison chart was the absence of projects like RabbitMQ or NATS Streaming. It seems that BullMQ is focused on ha ...

Focused on individual characters in a string to implement diverse CSS styles

Is there a way I can apply different CSS classes to specific indexes within a string? For example, consider this string: "Tip. \n Please search using a third character. \n Or use a wildcard." I know how to target the first line with CSS using : ...

Utilizing Jquery for independently blinking text counters

Whenever the user presses a button, a message is created and blinks 5 times. However, each time the button is pressed, all previous messages also blink along with the new one. The goal is to make only the new message blink individually 5 times upon each bu ...

The input tag contains an empty Request.Form

My issue lies in using Request.Form to retrieve text from an input field, as the value always ends up being empty. My goal is to extract the text value from the input tag and use it to query my database. I have attempted to write to an ASP TextBox but enco ...

Rejuvenate the table following an insertion or modification in AngularJS

My web application consists of a CRUD system that manages a list of users and their associated operations (such as insert, delete, etc.). When adding a new user, the user needs to fill out fields in a modal (using Bootstrap - UI). However, I am facing an i ...

Grunt tip: Converting jshint results to HTML format

I've set up jshint to run with grunt successfully, but now I'm looking to have the output in HTML format. Below is my grunt configuration: module.exports = function(grunt) { // Project configuration. grunt.initConfig({ jshint: { ...

Ensure that all of the text boxes are aligned perfectly in the same vertical position

Is there a way to align all the textboxes in the same vertical position without using tables, only using HTML and CSS? The textboxes should start just after the label and follow the width of the label. Also, all labels are left-justified. Code :: <d ...

Exploring new classes with jQuery's .not() and :not()?

I am working on a memory game where I need to flip cards and check if two are equal. My issue is that I do not want the function to run when clicking on a card that is already flipped, or on another flipped card. I tried using jQuery's .not() and :no ...

Guide on organizing users into groups and filtering them using Firestore's reference field type

I currently manage a small group of employees with plans to expand in the future. To streamline operations, I am looking to implement a grouping feature that allows users to sort employees based on their assigned groups. Although I thought about using the ...

Utilizing keyboard shortcuts for webpage navigation (within a content script)

Just to clarify, I'm not looking for code assistance right now. I mostly need a starting point. My goal is to create a Chrome browser extension that enables me to use keyboard keys for navigation on a specific webpage. The page in question belongs t ...

The Child/Parent arguments in Typescript methods cannot be assigned

Why is this not working in TypeScript? class Parent { id: string = '' } class Child extends Parent{ name: string = '' } const fails: (created: Parent) => void = (created: Child) => { return }; const failsToo: ({ create ...

Verify the identity of all REST API requests without the need for a username or password in order to obtain a

I have a unique setup where I am selling products. The API fetches product data from a centralized node back-end and displays it on an angular front-end that is hosted on multiple domains. The challenge I'm facing is the need to authenticate all reque ...

Ways to transfer input element values from a dynamically created HTML form using ng-repeat to the ng-controller

I just completed designing a form that looks like this: <form id="123"> <div ng-repeat="x in data" class="[ form-group form-inline ]"> <input class="form-control input-md" type="checkbox" id="{{x.name}}" autocomplete="off" /> ...

In need of changing the date format post splitting

I need help converting a date from MM/DD/YY to YYYYMMDD The current code I have is giving me an incorrect output of 2211. How can I implement a check on the Month and Day values to add a leading zero when necessary? var arr = '1/1/22'; arr = NTE ...

Arrange components within the form

Just started experimenting with this a few days ago. In my form, there is a text field and a button that I want to align side by side at the center of the form. .row { width: 100%; margin-bottom: 20px; display: flex; flex-wrap: wrap; } .col-6 { ...