0

I'm working on a form to edit a row in a database table via REST/AngularJS. However I can't access the data, as I get a $scope.l undefined error on the web console. I wonder how would I be able to get the form working so I use the REST server to edit the existing content.

//CONTROLLER to edit a specific item 
countryApp.controller('EditLocation', function($scope, $http, $routeParams, $location) {  
//
 var id = $routeParams.locid; 
 $scope.activePath = null; 

 $http.get('http://localhost/slimtest2/location/' + id).success(function(data) { 
  $scope.location = data;

}); 

$scope.editrel = function() { 
  var emP = { 
      location_id : id, 
      location_title : $scope.l.location_title,
      location_latitude : $scope.l.location_latitude,
      location_longitude : $scope.l.location_longitude
   }


  //convert data to JSON string
  var lucy = JSON.stringify(emP); 
    alert(lucy); 

  $http.put('http://localhost/slimtest2/location/1/edit', lucy); 


}

}); 
2
  • 3
    Where is the declaration of $scope.l ? Commented Aug 7, 2015 at 12:06
  • you should declare $scope.l = {}; in your controller Commented Aug 7, 2015 at 12:27

1 Answer 1

1

It's likely that you have a child scope in the view somewhere so if you use something like:

<input ng-model="l.location_title">

if l isn't an object inherited from parent scope, angular will create that object within the current scope.

Adding the following declaration in controller should help:

$scope.l={};

Now if element is within a child scope the l object will already exist and be reference to object in controller

If this does not resolve problem will need to see more of the code you are using

Sign up to request clarification or add additional context in comments.

5 Comments

@charlieftl I no longer get the undefined error on my web console. I am getting [object object] when I put a alert(emP) to test the form inputs.
that sounds good! Note you don't need to stringify the data to send it, $http will do handle that for you
@charlieleft I'm still going to have to figure getting the form inputs to display on the alert. Thanks a bunch. :)
you can't alert an object...log it to console instead
@charlieftl I just changed it to console.log(this.l.location_title) and it works. Hope this helps others.

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.