I'm experiencing some problems with AngularJS and $timeout.
I need to run some code, after the page is loaded. Http.get() loads items and ng-repeat displays them in table. Then I need to run the code to highlight row when I click it.
I found a solution for that using $timeout.
$timeout(function () {
console.log('in timeout');
$scope.highlightRows();
});
this does work, but not every time. In the function I log number of rows in the table and sometimes I get 0, therefore the click handler is not registered and it highlighting doesnt work.
$scope.highlightRows = function () {
console.log($("#tripsTable").children("tbody").children("tr").length);
$("#tripsTable").children("tbody").children("tr").children().click(function () {
console.log('row click');
$("#tripsTable").children("tbody").children("tr").removeClass("focusedRow");
$(this.parentNode).addClass("focusedRow");
});
};
when try to simulate I have to refresh by Ctrl + F5.
console log:
in timeout tripsController.js:14
0
I couldn't find any solutions to this problem, any suggestions will be appreciated :)
Thanks
Mark
EDIT: this is my HTML
<table class="table table-bordered" id="tripsTable">@*table-hover*@
<thead>
....
</thead>
<tbody>
<tr ng-repeat="trip in tripsVM.tripsList | orderBy: 'startDate'" ng-class="(trip.tripID == 0) ? 'newRow' : ''" class="row">
....