我是AngularJS的新手,觉得很有趣,但是对于以下情况我不太清楚。
app.factory('deleteFac', function($http){ var factory = {}; factory.edit = function(id){ $http.get('?controller=store&action=getDetail&id=' + id). success(function(data, status){ /** got an error on the following when i use return data; and i get data undefined in the controller which i get it because its doing a ajax call you don't get data until the call first. **/ $scope.detail = data; }) } return factory; })
我分配$scope并使用返回数据时遇到错误,无论如何我可以将返回数据分配给$scope吗?
$scope
您通常不在$scope工厂,服务或提供商内部使用。通常,您将返回promise(由返回$http),然后在控制器(您确实有$scope)中处理promise 。
promise
$http
factory.edit = function(id){ return $http.get('?controller=store&action=getDetail&id=' + id); }
控制器功能:
$scope.edit = function(id) { deleteFac.edit(id).then(function(response) { $scope.something = response.model; }); }