Jeffrey Teruel
Jeffrey Teruel

Reputation: 364

AngularJS $scope undefined

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); 


}

}); 

Upvotes: 0

Views: 1148

Answers (1)

charlietfl
charlietfl

Reputation: 171669

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

Upvotes: 1

Related Questions