Angularjs not outputting data on root vegetables $

I got stuck with some problems, actually I was in the solution, the problem was in the header, which prevents the response (like CORS problem), overcome with header and transformRequest like below. After that I got the webservice data but in one controller was using $ rootscope which displays some data id of the second method (API) for use in another controller to put on a third api to get data and I only get this after a minute it will throw an error : cannot read data "company data" about zero value, which is a field in the third api. when i used $ rootScope.Test.companies [0] .companyname which is stored data and unique across 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('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 to use the data of one controller to another, where I am using a different api that will get the date of the root of the parent. Please let me know if anyone knows about this or anything.

thank

+3


source to share


1 answer


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

  • Create a service for communication between them.
  • Use $ rootScope. $ broadcast

sample code

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

    }]);
});

      

and use it in your 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);
}
});

      

+1


source







All Articles