Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs not rendering $rootscope data

I am stuck on some problems , actually I was in problem solved , the problem was header which is not enableing to get response (like CORS issue) ,overcome by using header and transformRequest as shown in below code. After that I got webservice data but in one controller used $rootscope which render some of id of data of second method (API) to use in another controller to put on third one api to get data and I am getting this for only a minute then it will throw error : Cannot read property 'companies data' of null which is field in third api. when I used $rootScope.Test.companies[0].companyname which is store data, and unique for all api like 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('[email protected]'));
          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 to use one controller data to another where I am using another api which will get $rootscope date of parent. Please let me know if anybody know about that or anything

Thanks

like image 709
Code_S Avatar asked Sep 14 '26 15:09

Code_S


1 Answers

Yes you can use variables of one controller inside another controller using two methods

  1. Create Service to communicate between them.
  2. Use $rootScope.$broadcast

sample code

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

    }]);
});

and use it in controller like 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);
}
});
like image 91
Shubham Nigam Avatar answered Sep 16 '26 08:09

Shubham Nigam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!