javascript - How to pass a http request result in my case? -
i trying http request result kid controller.
i have like
<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 = info //do in parent controller here.... }) } } ]); angular.module('app').controller('childctrl', ['$scope', function($scope) { //i not sure how productdetail info here since it's http request call. } ]); angular.module('app').factory('myfactory', function($http) { var service = {}; service.request = function(id) { homecoming createproduct(id) .then(function(obj) { productid = obj.data.id; homecoming setproductdetail(productid) }) .then(getdetail) .then(function(productdetail) { homecoming productdetail.data }) } var createproduct = function(id) { homecoming $http.post('/api/product/create', id) } var setproductdetail = function(id) { homecoming $http.post('/api/product/setdetail', id) } var getdetail = function() { homecoming $http.get('/api/product/getdetail') } homecoming service; });
i able request result parentctrl not sure how pass kid controller. can help me it?
thanks!
potential approaches:
1) inject myfactory kid controller well.
2) access parent scope straight within childctrl:
$scope.$parent.productdetail
3) if wanting access html
$parent.productdetail
above assumes wanting access value separate potential version on kid scope (existing code doesn't show that).
if it's kid scope, , nil on kid scope (or scope in between) named productdetail, , you're not setting primitive value in kid scope name, should able see value straight through prototypical inheritance (but of 3 scenarios listed forcefulness need reference through parent).
javascript angularjs
No comments:
Post a Comment