4

I'm trying to get non-blank elements in my select using filter, but this doesn't work properly.

<select ng-model="selectedSubject.id">
    <option value="0">
        <%= res.getString("portlet.form.subjectField.default")  %>
    </option>  
    <option ng-repeat="sujet in subjects.allSubjects | filter:{name_fr:'!!'}" value="{{sujet.id}}">
        {{sujet.name_fr}}
    </option>
</select>
Luke Willis
  • 8,429
  • 4
  • 46
  • 79
beginner1417
  • 67
  • 1
  • 7

3 Answers3

5

filter:{my_field:'!!'} will filter out only null values.

If you want to filter out empty values, or both null and empty, you should use a custom filter

<option ng-repeat="sujet in subjects.allSubjects | filter:notEmptyOrNull" value="{{sujet.id}}">{{sujet.name_fr}}</option>

Controller

$scope.notEmptyOrNull = function(item){
  return !(item.name_fr === null || item.name_fr.trim().length === 0)
}
Mihai Dinculescu
  • 19,743
  • 8
  • 55
  • 70
1

The filter that you are using will only filter the data that contains null values not blank.

To filter out blank data you have to create filter.This question has already been answered on stack overflow. Visit this link.

Angular - Filter to remove blank strings from array

Community
  • 1
  • 1
0

You have to set the model so that angular selects the right option base on the select ngModel

<select ng-model="selectedSubject.id" ng-options="sujet.id as sujet.name_fr in subjects.allSubjects track by sujet.id"></select>

And set the default id on the controller.

$scope.allSubjects = subjects;
$scope.selectedSubject.id = subjects[0].id;
Raulucco
  • 3,406
  • 1
  • 21
  • 27