我正在研究一种通过REST / AngularJS在数据库表中编辑一行的表单。但是我无法访问数据,因为在Web控制台上出现$ scope.l未定义错误。我不知道如何才能使表单正常工作,因此我使用REST服务器来编辑现有内容。

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


}

});

最佳答案

您可能在某处的视图中有一个子作用域,因此,如果您使用以下方法:

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


如果l不是从父范围继承的对象,则angular将在当前范围内创建该对象。

在控制器中添加以下声明将有所帮助:

$scope.l={};


现在,如果element在子作用域内,则l对象将已经存在并被控制器中的对象引用

如果这样不能解决问题,则需要查看您正在使用的更多代码

关于javascript - AngularJS $ scope未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31877318/

10-10 01:17
查看更多