1

I've got declared a var called total = 10 in my controller. Now I want to show 10.

1 2 3 4 5 6 etc....

in my combobox. How could I do this I already tried this:

<div class="form-group">
        <label for="sel1">Aantal deelnemers:</label>
        <select class="form-control" id="sel1" ng-model="homeCtrl.selectedField"ng-options="for p in homeCtrl.totalFields"></select>
    </div>
Jamie
  • 10,302
  • 32
  • 103
  • 186

3 Answers3

1
ng-options="number for number in Array(homeCtrl.totalFields + 1).fill().map((x,i) => i)">

Edit:

For some reason the above syntax doesn't seems to work.

Here's another version

function Ctrl($scope) {
  $scope.homeCtrl = {totalFields: 10};
  $scope.selectedField = 0;
  $scope.totalFieldsArr = Array($scope.homeCtrl.totalFields + 1).fill().map((x, i) => i);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="Ctrl">
  <select ng-model="selectedField" ng-options="number for number in totalFieldsArr"></select>
  <div>You selected: {{selectedField}} </div>
</div>
Daniel
  • 6,194
  • 7
  • 33
  • 59
0

You show variables from controller trough $scope. so if you wanted to show your variable you would do:

$scope.total = total; In your controller.

There is another thing with forms in angular. Forms have its own scope. As for interacting with it from the controller I would recommend that you name your form with name tag:

<form name="myForm"> ...

Then you can access it from your controller like:

$scope.myForm

and instead binding to your scope you can bind to your FORM SCOPE like:

$scope.myForm.total = total; 

in your controller. Make sure that you do that after document is ready.

hope this helps, from your question I am not 100% what you are trying to do :)

DanteTheSmith
  • 2,937
  • 1
  • 16
  • 31
0
<div ng-app="myApp">
    <div ng-controller="control">
        <select>
        <option ng-repeat="i in getNumber(number) track by $index" value="{{$index+1}}"/>{{$index+1}}
        </select>
    </div>
</div>

script

var app = angular.module('myApp',[]);
app.controller('control',function($scope){

    $scope.number = 10;
    $scope.getNumber = function(num) {
        return new Array(num);   
    }
});
Bhavik vora
  • 274
  • 1
  • 3
  • 10