3

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

1 Answer 1

1

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);
}
});
Sign up to request clarification or add additional context in comments.

6 Comments

if I have nested $http and I want to return nested one data to controller , so what I would do? Please tell me.
What you need is $q.all. refer:stackoverflow.com/questions/17027196/…
$http({method: 'GET', url: '/api-one-url', data:{email: email,pwd:pwd }).then(function(apiOneData){ $http({method: 'GET', url: '/api-two-url', data:{userid:userid}}).then(function(apiTwoData){ // I want to save response in array to use in more than one controller console.log(apiOneData, apiTwoData); }); });
@shivam If you got help from this code upvote the answer and click the right icon on the answer
Can you solve my another issue? -->stackoverflow.com/questions/31408810/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.