0

I am trying to call text that has been added to an input using Angular JS, however in my console log I keep getting 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)
 }
})

2 Answers 2

1

You don't need to pass the parameter to the save function (and your definition of save doesn't include it). Change the button to:

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

Or either accept the parameter in the function declaration:

$scope.save = function(serverip){
 console.log(serverip)
}
Sign up to request clarification or add additional context in comments.

Comments

0

ng-repeat creates a new scope, inheriting from the scope above it. You are assigning serverip to this newly created scope. Thus, $scope in the favouritesController's context has no record of this. You should accept a parameter in your save function and log that (as you are passing this function the argument, anyways.)

Edit: or, as an alternative... 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.