I started my adventure with Angular directives. Somewhere in my app I have to choose number of passengers. Then I have to assign "First Name" and "Last Name" for each of them. So I created simple directive for creating html template for that and used it in ng-repeat:
app.directive('ngDoubleInputBox', function() { return { restrict: 'E', scope: { 'heading': '@?', 'leftInputHeading': '@?', 'rightInputHeading': '@?', 'headingNumber': '@?', 'passenger' : '=' }, templateUrl: '/src/html/doubleInput.html' } });
and doubleInput.html :
<div class="inputContainer"> <div class="containerTitle">{{headingNumber}}{{heading}}</div> <form class="form-inline"> <div class="form-group leftInputContainer"> <label for="left-input">{{leftInputHeading}}</label> <div> <input class="form-control" type="text" id="left-input" ng-model="passenger.firstName"> </div> </div> <div class="form-group rightInputContainer"> <label for="right-input">{{rightInputHeading}}</label> <div> <input class="form-control" type="text" id="right-input" ng-model="passenger.lastName"> </div> </div> </form> </div>
Usage:
<div class="flightPassengersContent" ng-repeat="passenger in getPassengers() track by $index"> <ng-double-input-box heading-number="{{$index + 1}}" heading=". Passenger" left-input-heading="First name" right-input-heading="Last name" model={{passenger}}> </ng-double-input-box> </div>
I do only have amount of passengers and empty array.
$scope.getPassengers = function() { return new Array($scope.passengersAmmount); };
So my question is: How to create an array of data that I could use here? Is it a good approach for passing array elements into directive and change them? If so, how to prepare such array? I tried to create some default passenger objects with null values and get rid of track by $index" statement but it didnt work.
Any ideas?
Regards, Michal
$scope.passengersAmmount
? The empty list of passengers should be created when that value is created or modified.