2016-07-26 64 views
0

我有一个搜索控制器和一个类别控制器和一个共享属性工厂。在我的类别控制器我设置sharedproperties.property但我无法检索搜索控制器的似乎是空的数据的数据,这里是我的代码无法将角度js中的数据从一个控制器移动到另一个控制器

.factory('sharedProperties', function() { 
     var property = ''; 

     return { 
      getProperty: function() { 
       return property; 
      }, 
      setProperty: function(value) { 
       property = value; 
      } 
     }; 
}) 


.controller("SearchCtrl",['config','sharedproperties',function(config,sharedproperties) 
{ 

$scope.search_subCategory_businesses= function() 
     { 
      $scope.bus_es = sharedProperties.getProperty(); 
      console.log("sharedProperty hahahaah is",sharedProperties.getProperty()); //logs empty instead of data 

      angular.forEach($scope.bus_es,function(value,key) 
         { 
          $scope.business = value; 

          if($scope.business.logo == '' || $scope.business.logo==null) 
          { 
           $scope.business.logo=config.BaseImageURL+"uploads/defbanner.png" 
          }else 
          { 
           $scope.business.logo=config.BaseImageURL+$scope.business.logo; 
          } 
          this.push($scope.business); 

         },$scope.businesses); 

     }; 

     //$scope.search_resultsFunction(); 
     $scope.search_subCategory_businesses(); 


}]) 



.controller("CategoryCtrl",['config','sharedproperties',function(config,sharedproperties) 
{ 

$scope.getBusinesses=function(sub_category_id) 
    { 
      $.get($scope.BaseURL+"classes/util.php?sub_category_id="+sub_category_id+"&transaction=get_businesses",function(results){ 

       alert("results are"+JSON.parse(results)); 
       $scope.bus_es =JSON.parse(results); 
       sharedProperties.setProperty($scope.bus_es);//successfully sets property 

       console.log("sharedProperty is",sharedProperties.getProperty()); 
       window.location.href=BaseURL+"search.php"; 
     }); 


    } 

}]) 
+0

它看起来不像'sharedProperties'包含在你的控制器中。 –

+0

您需要将服务注入您的控制器。现在你只注入'配置'? –

+0

以及我刚刚忘了发布时的问题,但他们被注入 – henrybbosa

回答

0

你混合jQuery的回调和角承诺。您应该通过$ http调用BackEnd并使用promise。为$ HTTP文档:https://docs.angularjs.org/api/ng/service/ $ HTTP

.factory('sharedProperties', function() { 
 
     var property = ''; 
 

 
     return { 
 
      getProperty: function() { 
 
       return property; 
 
      }, 
 
      setProperty: function(value) { 
 
       property = value; 
 
      } 
 
     }; 
 
}) 
 

 

 
.controller("SearchCtrl",['config','sharedproperties',function(config,sharedproperties) 
 
{ 
 

 
$scope.search_subCategory_businesses= function() 
 
     { 
 
      $scope.bus_es = sharedProperties.getProperty(); 
 
      console.log("sharedProperty hahahaah is",sharedProperties.getProperty()); //logs empty instead of data 
 

 
      angular.forEach($scope.bus_es,function(value,key) 
 
         { 
 
          $scope.business = value; 
 

 
          if($scope.business.logo == '' || $scope.business.logo==null) 
 
          { 
 
           $scope.business.logo=config.BaseImageURL+"uploads/defbanner.png" 
 
          }else 
 
          { 
 
           $scope.business.logo=config.BaseImageURL+$scope.business.logo; 
 
          } 
 
          this.push($scope.business); 
 

 
         },$scope.businesses); 
 

 
     }; 
 

 
     //$scope.search_resultsFunction(); 
 
     $scope.search_subCategory_businesses(); 
 

 

 
}]) 
 

 

 

 
.controller("CategoryCtrl",['$http','config','sharedproperties',function($http,config,sharedproperties) 
 
{ 
 

 
$scope.getBusinesses=function(sub_category_id) 
 
    { 
 
      $http.get($scope.BaseURL+"classes/util.php?sub_category_id="+sub_category_id+"&transaction=get_businesses").then(function(results){ 
 

 
       alert("results are"+JSON.parse(results)); 
 
       $scope.bus_es =JSON.parse(results); 
 
       sharedProperties.setProperty($scope.bus_es);//successfully sets property 
 

 
       console.log("sharedProperty is",sharedProperties.getProperty()); 
 
       window.location.href=BaseURL+"search.php"; 
 
     }); 
 

 

 
    } 
 

 
}])

更妙的是正在做,负责您的沟通与后端服务的HTTP通话。

+0

嗯谢谢@Kenneth,但我注意到,我的问题是,我要更新搜索控制器,我需要先调用一个函数,但实际上搜索控制器已经加载,所以我加载之前,该服务的属性set ..所以我仍然需要帮助才能走出这个陷阱 – henrybbosa

+0

最简单的解决方案是为你创建一个带有一些测试数据的plnkr,然后我可以看到逻辑并纠正它。 –

相关问题