我正在开发一个表单,通过 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/