2016-07-26 68 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,然後我可以看到邏輯並糾正它。 –

相關問題