仅显示AngularJS中的某些项目

时间:2015-06-14 02:18:41

标签: javascript angularjs ionic-framework

我是AngularJS和Ionic框架的新手,并使用基本Starter Tabs Ionic template,我希望能够#34;收藏/收藏"一些项目并将其显示在不同的选项卡上。

我的books.js结构如下:

.factory('Books', function() {

  // books data
  var books = [{
    id: 0,
    title: 'Sample Title',
    author: 'Sample Author',
    category: 'Horor, 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);
    },

现在,如果我想将该书添加到列表中,我应该创建一个新数组吗?或者angularJS是否提供某种类型的库,可以存储该数据而无需实际创建新列表?

我真的不确定如何解决这个问题。

更新#1:在查看Andy的示例后,我对controllers.js进行了以下修改

.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;
  };    
})

和按钮:

<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>

但使用以下ng-repeat:

ng-repeat="book in books | filter:{isFavorite:true}" 

我无法列出喜欢的书籍。

1 个答案:

答案 0 :(得分:0)

以下是一个例子:

IReadOnlyCollection<T>

观点:

IQueryable<T>

Here's a Plunker.