2016-07-26 11 views
0

私は検索コントローラとカテゴリコントローラと共有プロパティファクトリを持っています。私のカテゴリーコントローラでは、ここであなたはjQueryのコールバックと角度約束を混合している私のコード角度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

サービスをコントローラに注入する必要があります。今あなたは 'config'だけを注入していますか? –

+0

よく質問を投稿するときに忘れていましたが、注入されました – henrybbosa

答えて

0

である私はsharedproperties.propertyを設定するが、私はその空のデータであるように思わ検索コントローラ内のデータを取得するために失敗します。あなたは$ http経由でBackEndを呼び出し、約束を使用するべきです。 $ 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に感謝しますが、私の問題は検索コントローラを更新することです。検索コントローラが既に読み込まれているため、実際には関数を呼び出す必要があります。私はまだこのトラップの外に出る助けが必要です – henrybbosa

+0

最も簡単な解決策は、いくつかのテストデータでplnkrを作成することです、そして、私はロジックを見て修正することができます。 –

関連する問題