Reputation: 799
I am trying to get the http request data from a factory, process data in a parent controller and pass back to factory for my child controller to access it.
I have something like
parent controller
myFactory.makeApi(id)
.then(function(data) {
//process data here...
//I want to pass productDetail back to my factory and let
//my child controller use it
$scope.productDetail = productDetail;
})
Child controller
//I want to get the productDetail through parent' $scope.productDetail.
//but not sure how to get it.
myFactory.getProductDetail ???
myFactory
service.makeApi = function(id) {
return getProduct(id)
.then(function(httpObj) {
return httpObj.data;
})
}
var getProduct = function(id) {
return $http.post('/api/product/getProduct/' + id)
}
Basically I am not sure how to pass the productDetail to my child controller. Thanks for help.
Upvotes: 1
Views: 44
Reputation: 6061
By parent and child controller, I assume you mean that their views are nested.
If that is the case, the child $scope
prototypally inherits from the parent $scope
, so you can retrieve it in the child controller with $scope.productDetail
.
Upvotes: 1
Reputation: 20445
from parent controller you can broadcast an datrecieved event like
$scope.broadcast("datarecieved", data);
in child you can recived this data
$scope.$on("datarecieved",function(data){
//do something
})
In your case yo can do this
Parent Code
myFactory.makeApi(id)
.then(function(data) {
//process data here...
//I want to pass productDetail back to my factory and let
//my child controller use it
$scope.productDetail = productDetail
$scope.broadcast("datarecieved", productDetail);
})
in child
$scope.$on("datarecieved",function(data){
//do something
})
Upvotes: 1