小编Mar*_*ara的帖子

AngularJS $ timeout有时候工作有时不会

我遇到了AngularJS和$ timeout的一些问题.
在页面加载后我需要运行一些代码.Http.get()加载项目,ng-repeat在表格中显示它们.然后我需要运行代码以在我单击它时突出显示行.

我找到了使用$ timeout的解决方案.

$timeout(function () {
    console.log('in timeout');
    $scope.highlightRows();
});
Run Code Online (Sandbox Code Playgroud)

这确实有效,但不是每次都有效.在函数中我记录表中的行数,有时我得到0,因此点击处理程序没有注册,突出显示不起作用.

$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");
    });
};
Run Code Online (Sandbox Code Playgroud)

尝试模拟时我必须按Ctrl + F5刷新.

控制台日志:

in timeout tripsController.js:14
0 
Run Code Online (Sandbox Code Playgroud)

我找不到这个问题的任何溶液,有什么建议可以理解的:)
谢谢你
马克

编辑: 这是我的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">
            ....
Run Code Online (Sandbox Code Playgroud)

browser-refresh angularjs angularjs-ng-repeat angularjs-timeout

2
推荐指数
1
解决办法
6245
查看次数