我是Angular的新手,所以如果你问这个问题:"为什么不要......?"答案是......因为我不知道我能做到。
让工厂进行API调用,然后将该工厂注入到将在整个页面上具有范围的父控制器。然后让子控制器嵌套并从父控制器继承。
这是我到目前为止所拥有的。我可能会在这里离开,如果是这样的话,请告诉我。我正在独自努力,没有任何帮助,所以欢迎任何帮助。
谢谢。
var app = angular.module(' myApp',[]);
app.factory('myFactory', function($http){
var MyFactory = function(){};
MyFactory.getParams = function(){
return $http.get('/getparameters');
.success(function(data){
var roomname = data.roomname;
})
MyFactory.getRoom(roomname);
};
MyFactory.getRoom = function(room){
return $http.get('/my/api/' + room);
};
});
app.controller('RoomCtrl', function($scope, myFactory){
$scope.data = myFactory;
});
答案 0 :(得分:0)
你不需要使用资源,你需要使用承诺; 的 EDITED 强> 我试图让你更清楚
app.factory('apicall', ['$q','$http',function($q, $http){
function getRoom(options){
var promise = $http.get(options.paramUrl)
.success(function(data){
//handle your error
return $http.get(options.roomUrl + data.roomname);
})
.error(function(msg){
return $q.when(msg)
})
return promise;
}
return {getRoom:getRoom};
}])
如果你想在你想要的地方打电话给你的工厂
app.controller('RoomCtrl', ['$scope','apicall',function($scope, apicall){
var options = {
paramUrl:'/getparameters',
roomUrl:'/my/api/'
}
apicall.getRoom.all(function(result){
$scope.data = result;
})
}]);
答案 1 :(得分:0)
我通常会在返回$resource
的不同工厂中分隔不同的API调用。
例如,假设我们有2个不同的API调用指向不同的资源:
yourwebsite.com/user/:userId - 返回有关用户的数据
yourwebsite.com/photo/:photoId - 返回部分照片的数据
在角度上,你会将它们分成两个不同的工厂: "使用严格的&#34 ;;
angular.module("App.services").
factory("User",["$resource", function($resource){
return $resource("yourwebsite.com/user/:userId"),{userId:"@userId"});
}]);
和第二
angular.module("App.services").
factory("Photo",["$resource", function($resource){
return $resource("yourwebsite.com/photo/:photoId"),{photoId:"@photoId"});
}]);
在你的控制器中,你会像这样使用它们:
angular.module("App.controllers").controller("TestController",["User","Photo", function(User, Photo){
User.get({
id:10
}).$promise.then(function(data){
console.log(data); //returns the data for user with the id=10
});
Photo.get({
id:123
}).$promise.then(function(data){
console.log(data);
});
}]);
通常$resource
会映射CRUD API(我上面发布的是GET调用的基本示例)。查看$resource
上的documentation - 它已经有基本GET
,PUT
,POST
,DELETE
函数。
如果您对该网址只有1个简单操作而不是全部4个操作,我建议您使用$http
。然后,您可以在控制器中注入$http
并在那里执行请求,而不是为其创建工厂。
如果您有2个请求并且它们被链接(第二个请求取决于从第一个接收的数据),您必须等到第一个请求被解析。使用$resource
,您可以通过以下方式执行此操作:
angular.module("App.controllers").controller("TestController",["User","Photo", function(User, Photo){
var user_promise = User.get({
id:10
}).$promise.then(function(data){
console.log(data); //returns the data for user with the id=10
return data;
});
var photo_promise = Photo.get({
id:123
}).$promise.then(function(data){
console.log(data);
return data;
});
user_promise.then(photo_promise).
catch(function(error){
console.log(error); //catch all errors from the whole promise chain
});
}]);
答案 2 :(得分:0)
$ q服务可以帮助您处理两个异步调用的组合
app.factory('apicall', function($q, $http) {
var deferred = $q.defer();
$http.get('/getparameters')
.success(
function(data) {
var roomname = data.roomname;
$http.get(baseURL + 'room/' + roomname)
.success(
function(roomData) {
deferred.resolve(roomData);
}
)
.error(
function() {
deferred.reject();
}
)
})
.error(
function() {
deferred.reject();
}
);
return deferred.promise;
});
这是控制器,您可以使用您的服务
app.controller('someCtrl', function(apicall) {
apicall.then(
function(roomData) {
//success
},
function() {
//error
}
)
})