Search code examples
javascriptangularjsangularjs-service

Function / Class as Angular service


I am trying to inject a paging service on Angular controllers so I have:

angular.module("app").factory("paging", paging);

function paging() {  

  var paging = function (pageNumber, pageSize, itemCount) {    
    this.itemCount = itemCount;
    this.pageNumber = pageNumber;
    this.pageSize = pageSize;
    this.pageCount = itemCount > 0 ? Math.ceil(itemCount / pageSize) : 0;
  }

  paging.prototype.itemCount = 1
  paging.prototype.pageCount = 1
  paging.prototype.pageNumber = 1   
  paging.prototype.pageSize = 1

  paging.prototype.isFirst = function () {
    return this.pageNumber === 1; 
  }

  paging.prototype.toFirst() = function () {
    this.pageNumber = 1;
  }

  return paging;

}

I keep getting the error "paging.prototype.toFirst is not a function".

What am I doing wrong?


Solution

  • It is throwing an error because you are calling toFirst function before creating it, Rather I can say its syntactical mistake. You should creating function rather than calling it.

    paging.prototype.toFirst = function () {
        this.pageNumber = 1;
    }