如何在AngularJS中的工厂声明中使用$ scope?

时间:2014-07-03 18:33:48

标签: angularjs coffeescript angularjs-scope

books_ctrl.js.coffee

myApp.factory "Book", ($resource) ->
   $resource("/books/:id", {id: "@id", page: 3})

上述工厂声明工作正常。现在为了让我的分页工作,我希望将$ scope中的值包含到$ resource-component中。有点像:

myApp.factory "Book", ($resource) ->
   $resource("/books/:id", {id: "@id", page: $scope.currentPage})

但是会返回此错误:

ReferenceError: $scope is not defined

完整代码:books_ctrl.js.coffee

myApp.factory "Book", ($resource) ->
   $resource("/books/:id", {id: "@id", page: $scope.currentPage}, {})

myApp.controller "BooksCtrl", ($http, $scope, Book) ->
  $scope.init = () ->
    $scope.currentPage = 1
    $scope.getBooks()

  $scope.getBooks = () ->
  Book.query().$promise.then (books) ->
    $scope.books = books

  $scope.setPage = (newPage) ->
    newPage = 1 if newPage < 1
    $scope.currentPage = newPage
    $scope.getBooks()

0 个答案:

没有答案