Currently, I am attempting to retrieve text input through the use of AngularJS

Having trouble retrieving text input values using Angular JS? The console keeps showing undefined.

<div ng-controller="favouritesController" class="col-xs-12 favList">
  <input type="text" ng-model="newFav" ng-keyup= "add($event)" class="col-xs-12 FavInput" placeholder="Add A Favourite">
  <ul>
  <li ng-repeat="weatherList in weatherLists" class="col-xs-12">
  <span class="col-xs-12 col-sm-8">{{weatherList._id + ' / ' + weatherList.place}}</span>
  <div class="col-xs-12 col-sm-2">
    <button type="button" name="button" class="deleFav" ng-click="delete(weatherList)">Delete</button>
    <div class="col-xs-12">
      <input type="text" ng-model="serverip"/>
      <button ng-click="save(serverip)">Save</button>
    </div>
  </div>
</li>
</ul>
</div>

js code

myApp.controller('favouritesController', function($scope, $http, $rootScope, $route, $location) {
 $scope.save = function(){
  console.log($scope.serverip)
 }
})

Answer №1

There is no need to include the parameter in the save function (and it's not defined that way). Just update the button to:

<button ng-click="save()">Save</button>

Alternatively, you can modify the function declaration to accept the parameter:

$scope.save = function(serverip) {
 console.log(serverip)
}

Answer №2

Using ng-repeat creates a separate scope that inherits from the one above it. When you assign serverip to this new scope, the $scope in the favouritesController's context does not have access to it. One solution is to pass a parameter to your save function and log it (since you are already passing arguments to the function).

Edit: Alternatively, you can expose a property that will be inherited by ng-repeat:

Controller:

myApp.controller('favouritesController', function($scope, $http, $rootScope, $route, $location) {
  $scope.ip = { serverip:'' };
  $scope.save = function(){
    console.log($scope.ip.serverip)
  }
});

Template

<div ng-controller="favouritesController" class="col-xs-12 favList">
  <input type="text" ng-model="newFav" ng-keyup= "add($event)" class="col-xs-12 FavInput" placeholder="Add A Favourite">
  <ul>
  <li ng-repeat="weatherList in weatherLists" class="col-xs-12">
  <span class="col-xs-12 col-sm-8">{{weatherList._id + ' / ' + weatherList.place}}</span>
  <div class="col-xs-12 col-sm-2">
    <button type="button" name="button" class="deleFav" ng-click="delete(weatherList)">Delete</button>
    <div class="col-xs-12">
      <input type="text" ng-model="ip.serverip"/>
      <button ng-click="save()">Save</button>
    </div>
  </div>
</li>
</ul>
</div>

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

Tips for dynamically updating values when input numbers are modified using JavaScript

Check out this amazing tip calculator on netlify. I successfully built it using html, scss, and javascript without relying on any tutorials. Despite taking longer than expected due to being a beginner, I am proud of the outcome. Now, I need some assistanc ...

How come using a query object as a parameter for .limit() returns an empty array?

I am currently working on a mongoose query setup where I have a custom queryObject containing key-value pairs for specific records. If a key-value pair does not exist in the req.query, it is omitted from the queryObject. Oddly enough, when setting the que ...

Utilizing ngModel within the controller of a custom directive in Angular, instead of the link function

Is there a way to require and use ngModel inside the controller of a custom Angular directive without using the link function? I have seen examples that use the link function, but I want to know if it's possible to access ngModel inside the directive ...

I have exhausted all options trying to filter a 2D array in JavaScript

The 2D array provided is formatted as follows : [[ONE,1],[QUARTER,0.25],[QUARTER,0.25]] The desired output should be : [[ONE,1],[QUARTER,0.5]] An attempt was made using array.IndexOf but did not yield the expected result ONE,1,QUARTER,0.25,QUARTER,0.2 ...

React does not allow objects as a valid child element. This error occurs when an object with keys {nodeType, data, content} is found

I am encountering an issue with displaying content from Contentful on the server component. Everything is functioning correctly with no runtime errors, but I am receiving this specific error message: "Objects are not valid as a React child (found: object w ...

"How to set the header in AngularJS when opening a new window with a GET request URL

Can anyone provide guidance on setting headers and opening a URL (https://www.example.com) in a new window without including sensitive authentication information in the URL parameters? I am working with AngularJS for this task. I have searched through exi ...

Is it possible to quickly sync API data with a CSV file for updates?

My unique code allows retrieving Amazon product prices with the ASIN (Amazon Standard Identification Number). Here is the code snippet for reference. <?php class AmazonAPI { // Code implementation details here } ?> To display the price, use ...

Ways to implement material-ui button design on an HTML-native button

I am using pure-react-carousel which provides me an unstyled HTML button (ButtonBack). I would like to customize its style using material-ui. Trying to nest buttons within buttons is considered not allowed. An approach that works is manually assigning th ...

Utilize Vue.js to selectively display or manipulate objects with a

I am currently working on a Vue.js component page that receives data from my backend. The page consists of three different filters/states that users can choose from: Unlabeled, Completed, and Skipped. Unlablled Completed Skipped My approach involves usin ...

How to Handle Form Processing in AngularJS Before Submission

Consider the following scenario: There is a search box where users can enter two types of queries Scenario One: The search starts with "A1234" Scenario Two: The search starts with "B1234" Depending on whether it's Scenario One or Two, I need to cal ...

Choosing2 - incorporate a style to a distinct choice

Let's talk about a select element I have: <select id="mySelect"> <option>Volvo</option> <option value="Cat" class="red">Cat</option> <option value="Dog" class="r ...

Tips on dynamically changing the position of a div: Utilize absolute positioning within a for loop

Is there a way to extract the position x value of my div and store it in a variable? How do we then iterate over it within a for loop? <div id="object"></div> <style> #object{ position:absolute; width:10px; height:10px; ...

Using the typeof operator to test a Typescript array being passed as an object

I have a puzzling query about this particular code snippet. It goes like this: export function parseSomething(someList: string[]): string[] { someList.forEach((someField: string) => { console.log(typeof someField) }) Despite passing a s ...

Unlimited possibilities with parameters on an express server

I've set up React Router with recursive parameters, similar to the example here. On my Express server, I'm attempting to handle routes like /someRoute/:recursiveParameter? router.get('/someRoute/:recursiveParameter?', async (req, res ...

Click on another part of the page to reveal a dropdown menu

I am seeking a way to trigger the opening of this dropdown select not by directly clicking on its position, but rather by clicking on another <span>. Displayed below is the dropdown menu: <div class="styled-select"> <span id="camDurati ...

Angular 2 Error: AOT issue with base64 function not recognized

Currently, I'm attempting to compile my project using AOT. When I execute the command ngc -p tsconfig-aot.json Initially, it creates an aot folder with ngFactroy. However, when I modify the main ts file (as specified in the Angular documentation) to ...

Receiving a 500 status code upon converting a SqlDataReader into Json format

Getting a status 500 error and not sure where I am going wrong. When I click on the 'Getcustomers' button, the 'GetCustomers' method is called which returns JSON. Script: <script> var MyApp = angular.module("MyApp", []); ...

A mysterious token, $attrs, is an angular directive that seems to be used for comparing

I've been diving into mean stack development and following a video tutorial to enhance my skills. Progress was smooth until I encountered an issue while trying to compare two password fields: "Password" and "Confirm Password". Even though I'm rep ...

Refreshing Templates in Angular with ui.router

Initially, I apologize for the lengthy question, but it is necessary. Regrettably... I have multiple templates and routing logic stored in separate files. Strangely, the template I wish to load (depending on the selected region) does not initially load it ...

Attempting to transform HTML code received from the server into an image, but encountering an error while using ReactJS

This app is designed to automate the process of creating social media posts. I have a template for the vertical "Cablgram" stored in the backend, and when I make a request, it returns the HTML code for that template. However, I encounter an error when tryi ...