2016-07-26 3 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

서비스를 컨트롤러에 삽입해야합니다. 이제 '설정'만 주입하고 있습니까? –

+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 고맙습니다.하지만 저의 문제는 검색 컨트롤러를 업데이트하는 것입니다. 검색 컨트롤러가 이미로드되어있어 실제로 서비스의 속성이로드되기 전에 함수를 호출해야합니다. set .. 그래도이 함정에서 벗어나려면 여전히 도움이 필요합니다. – henrybbosa

+0

가장 쉬운 해결책은 일부 테스트 데이터로 plnkr을 작성한 다음 논리를보고 수정하는 것입니다. –

관련 문제