Angularjs不渲染$ rootscope数据

时间:2015-06-20 09:02:14

标签: javascript angularjs node.js angular-ui-router ionic-framework

我遇到了一些问题,实际上我遇到了问题,问题是标题是无法获得响应(如CORS问题),通过使用header和transformRequest来克服,如下面的代码所示。之后我得到了webservice数据,但在一个控制器中使用了$ rootscope,它渲染了第二种方法(API)的一些数据,用于另一个控制器,以便在第三个api上获取数据,我只得到了一分钟然后它会引发错误:无法阅读财产和公司数据' null是第三个api中的字段。当我使用$ rootScope.Test.companies [0] .companyname这是商店数据,并且对于所有类似api的主键都是唯一的。

   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;


              }            
          });

所以请告诉我如何将一个控制器数据用于另一个我使用另一个api的控制器数据,这将获得父本的$ rootscope日期。 如果有人知道这个或任何事情,请告诉我

由于

1 个答案:

答案 0 :(得分:1)

是的,您可以使用两种方法在另一个控制器内使用一个控制器的变量

  1. 创建服务以在它们之间进行通信。
  2. 使用$ rootScope。$ broadcast
  3. 示例代码

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

    并在控制器中使用它:

    控制器1

    angular.module('myservice', []).controller('ctrl1',function($scope, msgBus) {
        $scope.sendmsg = function() {
            msgBus.serviceValue='Hello'; 
       }
    });
    

    控制器2

    angular.module('myservice', []).controller('ctrl2',function($scope, msgBus) {
    $scope.checkValue(){   
    alert( msgBus.serviceValue);
    }
    });