How to notify when the value of multiple inputs changes using an Angular repeater

I am currently utilizing Angular to generate multiple inputs based on user needs. Although this approach works well, I am struggling to implement a simple alert that would display the values of these inputs. I have managed to retrieve the input values without using the repeat function, but encounter issues once I introduce the repeat feature.

<div ng-controller="MainCtrl">
  <form id="quickPick-form1" class="list" ng-submit="submit()">
      <fieldset data-ng-repeat="choice in choices" class="clearfix">
          <input type="text" placeholder="Item" class="pull-left" ng-model="item">
          <button class="remove pull-left" ng-show="$last" ng-click="removeChoice()">X</button>
      </fieldset>
      <button id="quickPick-button1" style="font-weight:600;font-style:italic;" class="addfields button button-calm button-block button-outline" ng-click="addNewChoice()">Tap me to add an item!</button>
      <div class="spacer" style="width: 300px; height: 40px;"></div>
      <p>Have enough items added? Click Randomize</p>
      <button ng-click="showChoice()" id="quickPick-button2" class="button button-calm button-block">Randomize</button>
      <div class="spacer" style="width: 300px; height: 20px;"></div>   
  </form>
</div>
<script>
.controller('MainCtrl', function($scope) {
  $scope.choices = [{id: 'choice1'}, {id: 'choice2'}];

  $scope.addNewChoice = function() {
    var newItemNo = $scope.choices.length+1;
    $scope.choices.push({'id':'choice'+newItemNo});
  };

  $scope.removeChoice = function() {
    var lastItem = $scope.choices.length-1;
    $scope.choices.splice(lastItem);
  };

  $scope.item = null;
  $scope.showChoice = function(){
    alert($scope.item);
  };

});
</script>

Answer №1

Give this a shot

$scope.displayOptions = function(){
    alert($scope.options.split(" /n"));
};

element is not within your scope. You are currently working with options[] in your scope.

Answer №2

Take a look at this Plunker example. Make sure to update your model to choice.item.

      <form id="quickPick-form1" class="list" ng-submit="submit()" >
  <fieldset  data-ng-repeat="choice in choices" class="clearfix">
      <input type="text" placeholder="Item" class="pull-left" ng-model="choice.item">
      <button class="remove pull-left" ng-show="$last" ng-click="removeChoice()">X</button>
  </fieldset>
  <button id="quickPick-button1" style="font-weight:600;font-style:italic;" class="addfields button button-calm button-block button-outline " ng-click="addNewChoice()">Tap me to add an item!</button>
  <div class="spacer" style="width: 300px; height: 40px;"></div>
  <p>Have enough items added? Click Randomize</p>
  <button ng-click="showChoice()" id="quickPick-button2" class=" button button-calm button-block ">Randomize</button>
  <div class="spacer" style="width: 300px; height: 20px;"></div>   

Also, in your JavaScript:

      $scope.showChoice = function(){
    var myChoices = '';
    for(var i =0; i < $scope.choices.length; i++){
      myChoices = myChoices + ', ' + $scope.choices[i].item;
    }
    alert(myChoices)
  };

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

Which programming language is more suitable for developing a chat website - PHP or JSP?

My goal is to create a web-based chat application similar to Yahoo's, utilizing JSP/PHP with AJAX. I'm debating between using JSP or PHP for this project and looking for the advantages and disadvantages of each. Which do you think would be bette ...

Adjust the cursor in a contenteditable division on Chrome or Webkit

Is there a way to set the caret position in a contenteditable div layer? After trying different methods and doing some research online, I finally found a solution that works in firefox: function set(element,position){ element.focus(); var range= w ...

Using destructuring repeatedly for a single object property

At times, I engage in nested destructuring, where I go more than one level deep. It can be risky, but I always make sure the property exists to avoid encountering an error of undefined. Recently, I came across this scenario: const { match: { ...

Error in material mapping: glDrawElements is trying to access vertices that are out of range in attribute 2

I have developed an algorithm that allows users to input data or a function and then generates a graphical representation of the function. It essentially creates a surface map, similar to the one you can see here. The graphing functionality is working well ...

Is there a way to have the font size in an H1 adjust dynamically to fill the entire width of the H1 using just CSS?

My h1 Element <h1>Title Of Element<h1> The h1 has a width of 200px h1{width:200px;} Although the h1 is indeed 200px wide, the text inside does not fully utilize that width. I expected setting font-size to 100% h1{font-size:100%;} However, t ...

Why is my JavaScript code running but disappearing right away?

I've encountered an issue with the code I wrote below. It's supposed to display the user's names when they enter their name, but for some reason, the names keep appearing and disappearing immediately. What could be causing this problem? Her ...

mysql nodejs function is returning a null value

Kindly review the contents of the dbfn.js file /*This is the database function file*/ var db = require('./connection'); function checkConnection(){ if(db){ console.log('We are connected to the Database server'.bgGreen); ...

Having trouble with jQuery selecting a specific span class?

I'm attempting to use the (.class) selector to select the span element, but for some reason it's not working as expected. <div class="text-wrap"> <div class="text-list"> <div class="text-suggestion"> <span class="text ...

Updating the value of bound data in AngularJS after a specific time interval

I am having trouble changing a bound data value through a controller after a delay. Below is the simple code I am using: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script> & ...

Submitting Files with jQuery AJAX in the ASP.NET MVC Framework

Currently, I am working on an application that requires me to upload a file using AJAX. I have implemented the jQuery.form library for this purpose, but strangely, when the action is triggered, the controller receives an empty list of files. Below is the H ...

Changing form position dynamically based on selection can be achieved by following these steps

I am creating a website that functions as a compact Content Management System, enabling users to effortlessly modify most of the site's content. Within my rails application, I have established two models: Category and Subcategory. A Category has mult ...

Utilizing Ajax in conjunction with the func_get_args() function

Can php's func_get_args() be used to capture 'post(ed)' data from an Ajax function call? (Currently using json to post data). Example of a hypothetical Ajax call posting data: . url: '.../.../...?action=function' data: { someStri ...

Issue with Microsoft Azure and AngularJS: Chart not rendering data as expected

I've encountered an issue where I can view the data locally, but once I open the Windows Azure cloud service application, the Json data is no longer being passed into my chart (). The Console displays a message stating "Controller names should start w ...

Display all event date markers, even if some dates are missing

Using the FullCalendar plugin has been a great experience for me. I have managed to successfully read data from a JSON object with the following code: function press1() { var employees = { events: [] }; ...

Tips for adding additional text within a span element correctly

Looking for suggestions on how to create a post box with '#' tag feature using HTML. Here is the current code I have: $("#textinput").click(function() { setCursor("textinput", 0); $(this).css("opacity", "0.5"); }) // Rest of the JavaScr ...

Having trouble reloading a seekbar (input range) in Angular 7 with a function?

I am currently in the process of developing a music player using Angular 7, and below is the HTML code for my component: <div class="track-controller"> <small>{{musicPlayerService.getCurrentTime()}}</small> <div class="progress- ...

Starting a tab just once

I have successfully created 4 static HTML pages that include: Home About Services Contact Each page contains a link that leads to another static page named myVideo.html, which plays a video about me in a new tab. The link is available on every page so ...

The background image is not being properly applied by React's makeStyles

Even after attempting various methods to load the image for the backgroundImage property, it still does not appear on the page. Interestingly, loading external images (such as those from Google) works just fine. I experimented with the following: backgro ...

Issue with jQuery, Ajax, and Haml as js.erb files are not triggering

After researching various sources, it seems like this issue needs to be addressed once more. When coding in Rails3, I am attempting to incorporate smooth Ajax effects when a user attempts to create a post on a different element of my application. Here&apos ...

Leveraging Vue.js's computed properties to access and manipulate elements within an

I have a simple template that displays text from a wysiwyg editor using two-way data binding, as shown below: <template> <div> <quill-editor v-model="debounceText" :options="editorOptionProTemplate" > </qu ...