【发布时间】:2015-03-26 19:53:40
【问题描述】:
我在使用服务/工厂作为 REST 接口并在 create 方法的回调函数中访问 $scope 变量的当前数据时遇到问题:
controllers.controller('ResourceAddCtrl', ['$scope', ... 'Resource','ResourceComb',
function($scope, ...) {
...
$scope.selectedEntryId = {};
$scope.resource = new Resource();
$scope.addResource = function(){
$scope.resource.$create(function(response){
var id = response.id;
console.log($scope.selectedEntryId);
//ResourceComb.$create({res_id : id, rescomb_id : $scope.selectedEntryId},function(){});
});
};
}]);
问题是 $scope.selectedEntryId 在回调方法中始终是它的初始值,尽管它在 html 代码中与 ng-model 一起使用,并且值得到完美更新,因为 {{selectedEntryId}} 在键入 a 时会产生预期的输出bindet 输入字段中的数字。
任何人都知道我的错误在哪里,或者我如何解决这个问题?
【问题讨论】:
-
您可以在绑定到
selectedEntryId的位置发布您的HTML/模板吗?将selectedEntryId设置在$scope的子范围内的ng-model 是否? -
您创建了一个新资源,但从未向它提供任何数据。检查发送的请求。剩下的问题需要看html
-
相关 HTML-src 如下您可以在 Angular 文档中找到回调。
标签: javascript angularjs rest angularjs-scope angularjs-service