javascript - Angularjs not rendering $rootscope data -


i stuck on problems , in problem solved , problem header not enableing response (like cors issue) ,overcome using header , transformrequest shown in below code. after got webservice data in 1 controller used $rootscope render of id of data of second method (api) use in controller put on third 1 api data , getting minute throw error : cannot read property 'companies data' of null field in third api. when used $rootscope.test.companies[0].companyname store data, , unique api primary key.

   var request = $http({         method: "post",         url: "http://app.xyz/xyzapp/public/user/login",         headers: {'content-type': 'application/x-www-form-urlencoded'},         transformrequest: function(obj) {           var str = [];            str.push(encodeuricomponent('email_id') + "=" + encodeuricomponent('demo@xyz.com'));           str.push(encodeuricomponent('password') + "=" + encodeuricomponent('demo@xyz'));           return str.join("&");         },                  });        request.success(function( response ) {          console.log("hiiiii::::"+json.stringify(response,status));         if (response.status=="success"){             $rootscope.test1=response.user_id;            var request1 = $http({               method: "post",               url: "http://app.xyz/xyzapp/public/company/getusercompanylist",               headers: {'content-type': 'application/x-www-form-urlencoded'},               transformrequest: function(obj) {                 var str = [];                  str.push(encodeuricomponent('user_id') + "=" + encodeuricomponent(response.user_id ));                 // str.push(encodeuricomponent('password') + "=" + encodeuricomponent('demo@123'));                 return str.join("&");               }                       });   // getcompany           request1.success(function( response ) {                console.log("hiiiii::::"+json.stringify(response,status)+"   "+response.companies.length+":length");               if (response.status=="success"){                 // alert(1);                 $state.go('tabdash');                 $rootscope.test = response;                 }                       }); 

so please tell me how use 1 controller data using api $rootscope date of parent. please let me know if know or

thanks

yes can use variables of 1 controller inside controller using 2 methods

  1. create service communicate between them.
  2. use $rootscope.$broadcast

sample code

angular.module('myservice', []).service('msgbus', function() {         this.servicevalue= {};      }]); }); 

and use in controller this:

controller 1

angular.module('myservice', []).controller('ctrl1',function($scope, msgbus) {     $scope.sendmsg = function() {         msgbus.servicevalue='hello';     } }); 

controller 2

angular.module('myservice', []).controller('ctrl2',function($scope, msgbus) { $scope.checkvalue(){    alert( msgbus.servicevalue); } }); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -