javascript - Angularjs 下一个和上一个按钮?

标签 javascript angularjs

我正在尝试以 Angular 创建下一个和上一个按钮。我是编程新手。我写了一个程序。如果我使用自定义数组 $scope.data = [] 它会起作用。当我使用 $http 时它不工作请帮助解决这个问题。

Controller.js

var app=angular.module('myApp', []);

app.controller('MyCtrl', ['$scope', '$http', '$filter', function ($scope, $filter, $http) {
    $scope.currentPage = 0;
    $scope.pageSize = 2;

    $scope.numberOfPages=function(){
        return Math.ceil($scope.pageSize);                
    }
    /*$scope.data = [
    { title: 'Reggae', id: 1 },
    { title: 'Chill', id: 2 },
    { title: 'Dubstep', id: 3 },
    { title: 'Indie', id: 4 },
    { title: 'Rap', id: 5 },
    { title: 'Cowbell', id: 6 }
  ];*/
               $http.get('https://raw.githubusercontent.com/bantic/imdb-data-scraping/master/data/movies.json')
          .then(function(response){
            $scope.data = response.data;
          });

}]);


app.filter('startFrom', function() {
    return function(input, start) {
        start = +start; //parse to int
        return input.slice(start);
    }
});

index.html

<div ng-app="myApp" ng-controller="MyCtrl">

    <ul>
        <li ng-repeat="item in data  | startFrom:currentPage*pageSize | limitTo:pageSize">
            {{item.title}}
        </li>
    </ul>
    <button ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">
        Previous
    </button>
    {{currentPage+1}}/{{numberOfPages()}}
    <button ng-disabled="currentPage >= getData().length/pageSize - 1" ng-click="currentPage=currentPage+1">
        Next
  </button>
</div>

输出

Previous {{currentPage+1}}/{{numberOfPages()}} Next

最佳答案

在 Controller 工厂函数中注入(inject)和使用它们时,您弄乱了依赖序列。确保注入(inject)的依赖项的使用顺序与注入(inject)的顺序相同。

您正在注入(inject) '$scope', '$http', '$filter' 并使用内部 Controller 工厂函数,例如 function($scope, $filter, $http) { 其中 $filter$http 的实例 & $http$filter 的实例。

app.controller('MyCtrl', ['$scope', '$filter','$http', function ($scope, $filter, $http) {

关于javascript - Angularjs 下一个和上一个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39315330/

相关文章:

javascript - 如何更新 ng-repeat 中的过滤器功能

java - @RequestBody 不适用于 Rest 服务

javascript - AngularJS 动态 TemplateUrl 值

javascript - 如何将commonservice文件中的对象的值获取到html

javascript - 如何指定 DOM 中子组件的位置?

javascript - const {name, value} = event.target -- 这是什么意思

javascript - 从 JavaScript 中的文件读取数据后更新 View

javascript - 带有 $http 请求的 Angular js 中的加载时间百分比

javascript - 更改函数中的变量并保留它的javascript?

javascript - 在 JS 中动态创建时在 CSS 中的样式