angularjs - How do perform an "except" filter in Angular? -
suppose have angular view allows user check books out of library. data model consists of 2 arrays of book
entities each have unique id field plus title field. first array contains entity every book in library , second array contains entity every book user has checked out.
librarybooks = [{ id: 0, title: "the adventure of tom sawyer"}, { id: 1, title: "moby dick" }, { id: 2, title: "to kill mockingbird" }, { id: 3, title: "the 3 little pigs" }]; checkedoutbooks = [{ id: 0, title: "the adventure of tom sawyer"}, { id: 3, title: "the 3 little pigs" }];
in short, library has 4 books , user has checked out two. if want list books both arrays, can write this:
<h1>library books</h1> <div ng-repeat="book in librarybooks"> {{ book.title }} </div> <h1>checked out books</h1> <div ng-repeat="book in checkedoutbooks"> {{ book.title }} </div>
suppose want display third list: subset of library books user has not checked out.
i have seen examples angular "filter" used specify 1 particular value should not matched in order narrow down list, in case, want exclude multiple values, how go doing this?
i have seen examples custom filter added angular module, think in case, custom filter should scoped controller.
i've got figured out. solution write filter function , attach $scope so:
function filter_notcheckedout(book) { var i; (i = 0; < that.librarybooks.length; += 1) { if (that.librarybooks[i].id === page.id) { return false; } } return true; }
in view, can referenced this:
<h1>books not checked out</h1> <div ng-repeat="book in librarybooks | filter:filter_notcheckedout"> {{ book.title }} </div>
Comments
Post a Comment