我正在使用表单通过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);
}
});
答案 0 :(得分:1)
你可能在视图中有一个子范围,所以如果你使用类似的东西:
<input ng-model="l.location_title">
如果l
不是从父作用域继承的对象,则angular将在当前作用域内创建该对象。
在控制器中添加以下声明应该有所帮助:
$scope.l={};
现在,如果element在子范围内,l
对象将已存在,并且是对控制器中对象的引用
如果这不能解决问题,则需要查看更多正在使用的代码