javascript - AngularJS $范围未定义

标签 javascript json angularjs sql-update

我正在开发一个表单,通过 REST/AngularJS 编辑数据库表中的一行。但是我无法访问数据,因为我在 Web 控制台上收到 $scope.l undefined 错误。我想知道如何才能使表单正常工作,因此我使用 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); 


}

}); 

最佳答案

您可能在 View 中的某处有一个子范围,因此如果您使用类似以下内容:

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

如果l不是从父作用域继承的对象,Angular 将在当前作用域内创建该对象。

在 Controller 中添加以下声明应该会有所帮助:

$scope.l={};

现在,如果元素位于子范围内,则 l 对象将已经存在并引用 Controller 中的对象

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

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

相关文章:

javascript - Angular 错误 $mdDialog.show 不是函数

javascript - 如何处理嵌套的 promise ?

javascript - 如何在 angularjs 中使用 ng-model 调用方法

javascript - 我如何 Jest 使用 ES6 依赖项

javascript - cloud 9.io JSON 中位置 0 的意外标记 m

python - 在 Python 脚本中运行时 SQL 查询返回空白输出

c - 允许在c中共享内存的json库

javascript - 如何在处理单词标记时解决 ƒ Object() { [native code] } 错误?

javascript - 如何在组合 API 的 setup() 中使用 $axios Nuxt 模块?

javascript - 引用错误: Invalid left-hand side in assignment JavaScript