2014-12-15 1 views
1

에 내 컨트롤러 내부에이 코드가 있습니다전화 공장은 첫 번째로드

내가 필요로하는 일
$scope.customers = []; 
$scope.categories = []; 

function init() { 
    partnersFactory.getPartners(categoryId) 
    .then(function(res){ 
     $scope.partners = res; 
    },function(status){ 
     //err 
    }) 

    partnersFactory.getCategories() 
    .then(function(res){ 
     $scope.categories = res; 
    },function(status){ 
     //err 
    }) 
} 

, 초기로드에 한 번만) partnersFactory.getCategories를 (실행하는 것입니다, 다음이 변경되지 않고 저장합니다. 가장 좋은 방법은 무엇입니까? var initialization = false와 같은 변수를 사용하면 매우 깨끗해 보이지 않습니다.

편집 목적 : 일부 데이터가 범주별로 정렬되어 있습니다. 내가 지금하고있는 일은 누군가가 카테고리를 클릭하면 서버에서 데이터 세트를 다시로드하지만 카테고리 (서버 측)로 필터링합니다. 그래서 데이터를 다시로드해야하지만 카테고리는 다시로드하지 않아야합니다. 내가 잘못하고 있니? 아마 angularjs 안에 필터하는 것이 더 낫지 ?? (서버의 데이터가 지속적으로 업데이트되지 않음).

+0

는 컨트롤러 함수를 호출하는 데 도움 희망 당신의 HTML에서 다음 데이터를 필터링 할 카테고리별로 정확한에? –

+0

그래서 카테고리를 클릭 할 때마다 init()을 호출합니까? – ValentinH

+0

나는 그것을 얻지 못한다. init에서만 카테고리를 얻길 원한다. 그런 다음 사용자가 어딘가에 클릭하면 데이터 만 다시로드됩니다. – tanou

답변

1

무엇에 대해 : 한 번만 그렇게 컨트롤러가 될 것을 좋아

,

$scope.customers = []; 
$scope.categories = []; 

function init() { 
    partnersFactory.getPartners(categoryId) 
    .then(function(res){ 
     $scope.partners = res; 
    },function(status){ 
     //err 
    })  
} 

partnersFactory.getCategories() 
.then(function(res){ 
    $scope.categories = res; 
},function(status){ 
     //err 
}) 

을 실행합니다 init() 기능의 측면에서 partnersFactory.getCategories() 퍼팅

$scope.customers = []; 
$scope.categories = null; 

function init() { 
    partnersFactory.getPartners(categoryId) 
    .then(function(res){ 
     $scope.partners = res; 
    },function(status){ 
    //err 
    }) 

    if($scope.categories == null) { 
     partnersFactory.getCategories() 
     .then(function(res){ 
      $scope.categories = res; 
     },function(status){ 
      $scope.categories = []; 
     }) 
    } 
} 
+0

그래, 그 방법을 시도했지만 아무 것도 가져 오지 않습니다. 언제든지 컨트롤러를 실행하고 모든 것을 다시 초기화합니다. 게시하려면 내 수정보기 – Sobakinet

1

그리고 만약 당신이 필요합니다 $scope.categories의 깨끗한 복사본 angular.copy() 아래와 같이 사용

귀하의 공장에서3210
partnersFactory.getCategories() 
.then(function(res){ 
    $scope.categories = res; 
    $scope.originalObj = angular.copy($scope.categories); // don't do any changes to $scope.originalObj 
},function(status){ 
     //err 
}) 
+0

나는 내가하고 싶은 일을 더 분명하게 보여주기 위해 내 게시물을 편집했습니다. – Sobakinet

1

당신은 다음과 같이 떨어지게 할 수 있습니다 : 그것은 종류의 데이터를 현금으로해야

.factory('partnersFactory', function($http) { 
    var categories = []; 

    var getCategories = function() { 
     $http.get(...) 
      .success(function(data){ 
      categories = data; 
      }); 
    } 
    getCategories(); 

    return { 
     getPartners: function(categoryId) { 
      $http.get(...) 
       .success(onSuccess) 
       .error(onError); 
     }, 
     getCategories: function(){ 
      return categories; 
     } 
    }; 

. 당신의 HTML에서 다음

angular.module('yourApp').controller('yourCtrl', ['$scope', 'yourFactory', function ($scope, yourFactory) { 

    $scope.initPage = function() { 
    $scope.getCat(); 
    $scope.getData(); 
    } 

    $scope.getCat = function() { 
    ... 
    } 

    $scope.getData = function() { 
    ... 
    } 


    $scope.initPage(); 
}]); 

: 데이터를 얻기 위해이 방법 범주를 얻기를위한 하나, 다른 생성, 컨트롤러에

:

1

그럼, 당신이 필요로하는 것은 그 같은 것입니다 버튼/요소가 데이터를 다시로드하여 메소드를 호출하는 생각에 당신은 카테고리별로 정렬 할로서, (카테고리 GetData의에 PARAM를 추가하는 것처럼 당신이 뭔가를 할 수있는,

<div> 
    <button ng-click="getData"> reload </button> 
</div> 

을 NG 클릭) 당신이

<ul> 
    <li ng-repeat="cat in categories"> <a ng-click="getData(cat)"> {{ cat.name }} </a> </li> 
</ul> 

그것이

관련 문제