AngularJS $范围未定义
AngularJS $scope undefined
我正在制作一个表单,通过 REST/AngularJS 编辑数据库 table 中的一行。但是我无法访问数据,因为我在 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={};
现在如果元素在子范围内,l
对象将已经存在并引用控制器中的对象
如果这不能解决问题将需要查看更多您正在使用的代码
我正在制作一个表单,通过 REST/AngularJS 编辑数据库 table 中的一行。但是我无法访问数据,因为我在 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={};
现在如果元素在子范围内,l
对象将已经存在并引用控制器中的对象
如果这不能解决问题将需要查看更多您正在使用的代码