2014-02-12 2 views
1

ng-repeat 필터에서 사용할 검색 모델 개체를 설정하는 드롭 다운이 있습니다.각도가 내 필터를 지우지 않는 이유는 무엇입니까?

<select ng-model="search.location_id" ng-options="id as name for (id,name) in search_locations"> 
    <option value="">Location</option> 
</select> 

또한 '위치 정보'는 검색을 지우기위한 것입니다. 문제는 내가 목록을 올바르게 필터링하는 위치를 선택한 다음 기본 '위치'옵션으로 돌아가서 검색 필터를 지우지 않고 모든 ng-repeat 항목을 지우는 것입니다.

<tr ng-repeat="course in courses | filter:search"> 

물론 코스 객체에는 물론 course.location_id 속성이 있습니다. 'search_locations'는 {id : name, id : name} 형식으로 검색 할 위치 목록입니다.

어떻게 전체 목록을 다시 가져 옵니까? /이 특정 검색 매개 변수를 지우시겠습니까? 기본 옵션을 선택하면 search.location_id에 할당 된 값이 null하고 search.location_id === null 있다면, 당신의 courses 목록에 아무것도 일치하지 때문이다

답변

1

때 각 필터를 실행하고, 값은 정의 후 필터 단락이고, 전체 목록이 반환

$scope.search_locations = [ 
    { description: 'show all', location_id: undefined }, 
    { description: 'block 1', location_id: 1 }, 
    { description: 'block 2', location_id: 2 } 
]; 

데모 fiddle : 당신이 할 수있는 것은 undefined로 설정 location_id와 search_locations 배열에 추가 개체를 추가합니다.

+0

좋은 해결 방법. +1 :) –

0

(물론 course.location_id === null,하지 않는 한).

AngularJS docs의 예제는 기본값이 null임을 보여줍니다.

값이 검색되는 경우에 선택되는 것의 예 null이다 http://plnkr.co/edit/meKe7l6rubaHEN1eRbzC?p=preview

템플릿 :

<body ng-controller="MainCtrl"> 
    <p ng-repeat="item in items | filter:search"> 
     Id: {{item.id}}, Name: {{item.name}} 
    </p> 
</body> 

컨트롤러 :

app.controller('MainCtrl', function($scope) { 
    $scope.items = [{ 
    id: 1, 
    name: 'alice' 
    }, { 
    id: 2, 
    name: null 
    }]; 

    $scope.search = { 
    name: null 
    }; 
}); 

가 출력에만 제의 항목목록.

이 문제를 해결하려면 직접 filter 함수를 작성하십시오.

(필터 값은 전에 각 1.2 인 경우이 같은 문제였다)

+0

응답 해 주셔서 감사합니다.하지만 여전히 전체 ng-repeat 목록을 다시 가져 오는 방법을 모르겠습니다. 나는 이것이 공통점이 있어야한다고 생각합니다. – tommybananas

+1

@ snowman4415 이것은 [long] (https://github.com/angular/angular.js/issues/1780) [standing] (https://github.com/angular/angular.js/issues/5117)입니다. [Issue] (https://github.com/angular/angular.js/issues/5055)를 Angular와 함께 사용하고'false' 값처럼 행동하는 대신'null' 만'null'과 매치하기로 결정했습니다. 사용자 정의 방식으로 배열을 필터링하기 위해 함수를 작성하거나 **'ng-repeat'를 사용하여'model'에'string' 만'values'로 사용하기 때문에 원하는'option'을 생성 할 수 있습니다. 객체가 아닙니다. –

관련 문제