在我的案例中,如何传递http请求结果
How to pass a http request result in my case?
我正在尝试将http请求结果发送给我的子控制器。
我有类似的东西
<div ng-controller = "parentCtrl">
<button ng-click="callApi()">click me</button>
<div ng-controller = "childCtrl">
<div>{{productDetail}}</div>
</div>
</div>
angular.module('App').controller('parentCtrl', ['$scope','myFactory',
function($scope, myFactory) {
$scope.callApi = function() {
myFactory.request(id)
.then(function(data) {
$scope.productDetail = data
//do something in parent controller here....
})
}
}
]);
angular.module('App').controller('childCtrl', ['$scope',
function($scope) {
//I am not sure how to get the productDetail data here since it's a http request call.
}
]);
angular.module('App').factory('myFactory', function($http) {
var service = {};
service.request = function(id) {
return createProduct(id)
.then(function(obj) {
productID = obj.data.id;
return setProductDetail(productID)
})
.then(getDetail)
.then(function(productDetail) {
return productDetail.data
})
}
var createProduct = function(id) {
return $http.post('/api/product/create', id)
}
var setProductDetail = function(id) {
return $http.post('/api/product/setDetail', id)
}
var getDetail = function() {
return $http.get('/api/product/getDetail')
}
return service;
});
我能够获得parentCtrl的请求结果,但我不确定如何将其传递给我的子控制器。有人能帮我吗?
谢谢!
潜在方法:
1) 将myFactory也注入到子控制器中。
2) 直接从childCtrl:中访问父作用域
$scope.$parent.productDetail
3) 如果想要从HTML 访问
$parent.productDetail
上面假设您希望访问该值,该值与子作用域上的潜在版本特别分离(现有代码没有显示这一点)。
如果它是一个子作用域,并且子作用域(或两者之间的作用域)上没有任何名称为productDetail,并且您没有在具有该名称的子作用域中设置基元值,那么您应该能够通过原型继承直接查看该值(但列出的三种情况中的任何一种都可能强制需要通过父级进行引用)。
相关文章:
- 节点如何将文件传递到http.write方法
- 如何将动态构建的字符串作为http头在http.call中与流星一起传递
- nodejs-如何安全地将令牌从http获取请求传递到html页面
- $http().在传递带有“?”的额外Get参数时获取SyntaxError&”;
- 在angular 2中向http get请求传递url参数
- 如何将$http响应值传递给另一个函数
- 如何在 JavaScript 的 HTTP 标头中传递 X-Auth-Token
- 如何在 NodeJS 中传递响应参数,包括来自 POST http.request 的正文
- UIWebView 中 PUT 或 POST 类型的 ajax 不传递 HTTP 正文
- Angular JS:在HTTP服务(POST)中传递输入参数
- 角度谷歌地图从$http传递坐标
- 如何在角度$http中传递 URL 参数
- Angular 2 Http 轮询未传递错误
- 将数据从输入字段传递到角度工厂中的 $http.get
- 如何使用 Angular-HTTP-AUTH 传递凭据
- 在我的案例中,如何传递http请求结果
- 我如何在层之间传递http服务器变量,而不是将其用作node.js中不同文件中的require语句
- 从Backbone.js向CodeIgniter向DB传递HTTP请求
- 如何在不删除cookie的情况下传递http错误400
- 始终传递$http.Post中的Post变量