我正在尝试从工厂获取 http 请求数据,在父 Controller 中处理数据,然后传回工厂供我的子 Controller 访问它。
我有类似的东西
父 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;
})
子 Controller
//I want to get the productDetail through parent' $scope.productDetail.
//but not sure how to get it.
myFactory.getProductDetail ???
我的工厂
service.makeApi = function(id) {
return getProduct(id)
.then(function(httpObj) {
return httpObj.data;
})
}
var getProduct = function(id) {
return $http.post('/api/product/getProduct/' + id)
}
基本上我不确定如何将 productDetail 传递给我的子 Controller 。感谢您的帮助。
最佳答案
从父 Controller 你可以广播一个datrecieved事件
$scope.broadcast("datarecieved", data);
在 child 中你可以收到这些数据
$scope.$on("datarecieved",function(data){
//do something
})
在你的情况下你可以这样做
家长代码
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);
})
child
$scope.$on("datarecieved",function(data){
//do something
})
关于javascript - 就我而言,如何将数据传递给另一个 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26694476/