【问题标题】:AngularJS simple page counter in controller控制器中的AngularJS简单页面计数器
【发布时间】:2013-10-10 05:13:31
【问题描述】:

我的 html 模板中有这个。我希望按钮在单击时触发 showMore 功能。

<button ng-click="showMore(secondPage)">Show More</button>

这是控制器。我不确定为什么 $scope.nextPage 除了在 $scope.secondPage 之外的任何地方都会增加。我希望$scope.secondPage 在第一次单击时触发Entry.query({page: 2}),然后在下次单击时触发Entry.query({page: 3})。但现在它一直在触发第 2 页。

@MainController = ["$scope", "Entry", ($scope, Entry) ->
  $scope.entries = Entry.query({page: 1})

  $scope.nextPage = 2
  $scope.secondPage = Entry.query({page: $scope.nextPage})

  $scope.showMore = (secondPage) ->
    i = 0
    while i < secondPage.length
      $scope.entries.push(secondPage[i])
      i++
    $scope.nextPage++
    console.log("nextpage" + $scope.nextPage)    

]

我正在使用咖啡脚本。我不完全明白为什么 $scope.secondPage 中的页码没有增加。

【问题讨论】:

    标签: ruby-on-rails angularjs coffeescript


    【解决方案1】:

    我愿意:

    <button ng-click="showMore()">Show More</button>
    
    @MainController = ["$scope", "Entry", ($scope, Entry) ->
    
      $scope.entries = []
      current_page = 1
    
      $scope.showMore = ->
        on_success = (entries)->
          $scope.entries = $scope.entries.concat entries
          current_page = current_page + 1
        Entry.query {page: current_page}, on_success
    
      $scope.showMore()
    ]
    

    【讨论】:

    • 太棒了!感谢你的回答。所以我不只是盲目地接受这个答案。我会试着理解它。这里的主要区别是您在 $scope.showMore 函数中调用 Entry.query,该函数将递增的 current_page 保持在与 Entry.query {page: current_page} 中的 current_page 参数相同的范围内
    猜你喜欢
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多