在AngularJS中显示列表按钮

时间:2017-06-28 13:04:47

标签: angularjs

我想添加一个按钮

if array.length! = 0

我写的内容不起作用。

$scope.pages = 0;
if(array.length != 0) {
   $scope.pages++;
}

在AngularJS中我做了:

  <div ng-repeat="page in pages">
    <button ng-click="loadProducts(1)">{{ page }}</button>
  </div>

我认为在AngularJS中可以不用数字,重复按钮

3 个答案:

答案 0 :(得分:1)

我认为你需要这个,

 <div ng-repeat="i in loadProducts(pages) track by $index">

<强>样本

&#13;
&#13;
var myApp = angular.module('ReqWebApp', [])

myApp.controller('ReqAppController', function ReqAppController($scope) {
$scope.pages = 4;
$scope.loadProducts = function(num) {
    return new Array(num);   
}
});
&#13;
<!DOCTYPE html>
<html ng-app="ReqWebApp">

<head>
    <meta charset="UTF-8">
    <title>New Request</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>

<body ng-controller="ReqAppController">
    <div ng-repeat="page in loadProducts(pages) track by $index">
    <button>test{{ page }}</button>
  </div>
</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果要创建按钮列表,请执行以下操作:

<强> JS

  $scope.pages = 0;
    $scope.pageArray=[];
    if(array.length != 0) {
     for(var i=0;i<array.length;i++){
       $scope.pages++;
       $scope.pageArray.push($scope.pages);
    }

    }

<强> HTML

<div ng-repeat="page in pageArray">
    <button ng-click="loadProducts($index)">{{ page }}</button>
 </div>

JS Fiddle http://jsfiddle.net/5dybgrvv/

答案 2 :(得分:0)

或者您可以在不向控制器添加内容的情况下执行此操作,如下所示:

&#13;
&#13;
var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
  $scope.number = 10;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <div ng-repeat="x in [].constructor(number) track by $index">
    <button ng-click="loadProducts(1)">{{ $index }}</button>
  </div>
</div>
&#13;
&#13;
&#13;