1

I want to add a button

if array.length! = 0

and what I wrote does not work.

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

and in AngularJS I made:

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

I think in AngularJS can't after number, repeat buttons

3
  • what exactly you want? Commented Jun 28, 2017 at 13:08
  • If $scope.pages = 4 print 4 buttons in html Commented Jun 28, 2017 at 13:08
  • ng-repeat works like you have multiple objects in an array. not on numbers. Commented Jun 28, 2017 at 13:09

3 Answers 3

1

I think you need this,

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

DEMO

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

myApp.controller('ReqAppController', function ReqAppController($scope) {
$scope.pages = 4;
$scope.loadProducts = function(num) {
    return new Array(num);   
}
});
<!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>

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to create a list of buttons, do the following:

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/

Comments

0

Or you can do it without adding content to your controller, like this :

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

function MyCtrl($scope) {
  $scope.number = 10;
}
<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.