I have a simple AngularJS page with different sections that I show & hide when links are clicked. One of these areas has a repeated list that can be filtered.
When the section containing the list is shows/hidden with ng-show
or ng-hide
it behaves normally. When ng-if
is used, the list cannot be filtered.
Demos
Sample HTML
<nav>
<a href="javascript:{}" ng-click="area='one';">Area 1</a>
<a href="javascript:{}" ng-click="area='two';">Area 2</a>
</nav>
<div ng-if="area==='one'">
<h3>Area 1!</h3>
<input type="text" placeholder="filter list..." ng-model="filterText" />
<ul>
<li ng-repeat="item in list | filter: listFilter">
{{item.id}} - {{item.name}}
</li>
</ul>
</div>
<div ng-if="area==='two'">
<h3>Area 2!</h3>
<p>Stuff here...</p>
</div>
Sample Angular
$scope.area="one";
$scope.filterText="";
$scope.list = [
{id:1, name:"banana"},
{id:2, name:"apple"},
{id:3, name:"orange"},
{id:4, name:"pear"},
{id:5, name:"apricot"}
];
$scope.listFilter = function(item){
var term = $scope.filterText.trim().toLowerCase();
return item.id.toString().indexOf(term) > -1 || item.name.indexOf(term) > -1;
};
ngModel
of the input, anyway). Hence duplicate of: https://mcmap.net/q/125953/-angularjs-ng-model-doesn-39-t-work-inside-ng-if . (To prove this to yourself, you can move the input outside of thengIf
, your example will work as expected). A working modification of yours would useng-model="$parent.filterText"
, jsfiddle.net/Lmbfdxvs/10 – Rupiahng-if
creates a new scope, but nesting insideng-show
does not? – AthistengIf
transcludes the contents, creating a new scope every time it is created (as in, if it becomes false, the scope is destroyed; when it becomes true again a new scope is created). docs.angularjs.org/api/ng/directive/ngIf .nsShow
andngHide
basically just toggle/animate classed. – Rupiah