As a newcomer to AngularJS and the Ionic framework, I'm currently working with the basic Starter Tabs Ionic template. I would like to implement a "Favourite/Bookmark" feature for certain items and display them on a separate tab.
The structure of my books.js
file is as follows:
.factory('Books', function() {
// books data
var books = [{
id: 0,
title: 'Sample Title',
author: 'Sample Author',
category: 'Horror, Fiction',
cover: '/cover.jpeg',
details: 'some details about the book',
chapters: [
{
id : 1,
name: 'Chapter 1',
filename: 'chapter1.html',
},
{
id : 2,
name: 'Chapter 2',
filename: 'Chapter2.html',
}
]
}
.....
return {
all: function() {
return books;
},
// remove a book from the list
remove: function(book) {
books.splice(books.indexOf(book), 1);
},
I am unsure about how to approach adding a book to a favorites list. Should I create a new array for this purpose, or does AngularJS provide a library that can store this data without creating a separate list?
Update # 1: Following Andy's example, I have updated the controllers.js
file as shown below:
.controller('DashCtrl', function($scope, Books) {
$scope.books = Books.all();
$scope.remove = function(book) {
Books.remove(book);
};
$scope.markFavorite = function(book) {
Books.isFavorite = true;
};
$scope.unmarkFavorite = function(book) {
Books.isFavorite = false;
};
})
and added buttons:
<ion-option-button class="button-assertive" ng-click="remove(book)">
Delete
</ion-option-button>
<ion-option-button class="button-positive" ng-click="markFavorite(book)">
Fav
</ion-option-button>
However, when using the following ng-repeat:
ng-repeat="book in books | filter:{isFavorite:true}"
I encounter an issue where I cannot list favorite books.