I have a problem with AngularJS and a function that is called before populating the data array. When my function is called in ng-init
, the $scope.bookings
array was not evaluated (filled with data), resulting in no data.
My goal: to receive all orders for a certain type of reservation and a certain date and display them all in <td>
Here is my HTML code:
Description: I look through all bookingTypes
, and then through all dates
. ng-init
is executed as many times as dates, and it works. otherBookings
should be an array of orders for this bookingType and this date. However, $scope.bookings
not populated with data, so the otherBookings
never runs.
<tr ng-repeat="bookingType in bookingTypes"> <td>{{bookingType.Name}}]</td> <td ng-repeat="date in dates" ng-init="otherBookings = checkOtherBookings(bookingType.ID, date)"> <span ng-repeat="otherBooking in otherBookings"> <a ng-href="/Bookings/Edit/{{otherBooking.Booking.ID}}"><span >{{otherBooking.Customer.FirstName}}</span></a> </span> </td> </tr>
Here is my JavaScript code:
Description: At the top of the BookingsController
I call a service that populates the $scope.bookings
array $scope.bookings
data, and the $scope.checkOtherBookings()
function is below:
BookingService.getAllBookings().then(function(data) { $scope.bookings = data.data; }); $scope.checkOtherBookings = function(bookingType, date) { console.log($scope.bookings); var newBookingArray = []; for(var i = 0; i < $scope.bookings.length; i++) { if($scope.bookings[i].Booking.Type == bookingType) { var tmpDateFrom = $scope.bookings[i].Booking.DateFrom; var tmpDateTo = $scope.bookings[i].Booking.DateTo; if(date >= tmpDateFrom && date <= tmpDateTo) { newBookingArray.push($scope.bookings[i]); } } } return newBookingArray; };
The $scope.checkOtherBookings()
function is called 22 times (as many times as the dates), but console.log($scope.bookings)
outputs 22 times []
- so my observation is that the $scope.bookings
empty every time when the function is called.
I need some kind of wait method to execute ng-init
until the $scope.bookings
array is filled with data.
Any ideas?
UPDATE 1 - 09/14/13
Now newBookingArray
has some data, but otherBookings
in ng-init
never get it.
HTML:
<tr ng-repeat="bookingType in bookingTypes"> <td>{{bookingType.Name}}</td> <td ng-repeat="date in dates" ng-init="otherBookings = checkOtherBookings(bookingType.ID, date)"> <span ng-repeat="otherBooking in otherBookings"> <a ng-href="/Bookings/Edit/{{otherBooking.Booking.ID}}"><span >{{otherBooking.Customer.FirstName}}</span></a> </span> </td> </tr>
JavaScript:
BookingService.getAllBookings().then(function(data) { $scope.bookings = data.data; }); $scope.checkOtherBookings = function(bookingTypeID, date) { var deferred = $q.defer(); $scope.$watch('bookings', function(bookings) { if(!bookings.length) return; var newBookingArray = []; for(var i = 0; i < $scope.bookings.length; i++) { if($scope.bookings[i].Booking.Type == bookingTypeID) { var tmpDateFrom = $scope.bookings[i].Booking.DateFrom; var tmpDateTo = $scope.bookings[i].Booking.DateTo; if(date >= tmpDateFrom && date <= tmpDateTo) { newBookingArray.push($scope.bookings[i]); console.log(JSON.stringify(newBookingArray)); } } } deferred.resolve(newBookingArray); }); return deferred.promise; };
UPDATE 2 - 09/14/13
It is allowed! I have moved the assignment from ng-init
to ng-repeat
and it works fine.
HTML:
<tr ng-repeat="bookingType in bookingTypes"> <td>{{bookingType.Name}}</td> <td ng-repeat="date in dates"> <span ng-repeat="otherBooking in checkOtherBookings(bookingType.ID, date)"> <a ng-href="/Bookings/Edit/{{otherBooking.Booking.ID}}"><span >{{otherBooking.Customer.FirstName}}</span></a> </span> </td> </tr>
JavaScript:
BookingService.getAllBookings().then(function(data) { $scope.bookings = data.data; }); $scope.checkOtherBookings = function(bookingTypeID, date) { var newBookingArray = []; for(var i = 0; i < $scope.bookings.length; i++) { if($scope.bookings[i].Booking.Type == bookingTypeID) { var tmpDateFrom = $scope.bookings[i].Booking.DateFrom; var tmpDateTo = $scope.bookings[i].Booking.DateTo; if(dateInRange(tmpDateFrom, tmpDateTo, date)) { newBookingArray.push($scope.bookings[i]); } } } return newBookingArray; };