anchorScroll 不在带有 ng-repeat 的控制器中执行?

anchorScroll does not execute in controller with ng-repeat?

我想我已经确定了一些关于 ng-repeat 运行 时间的奇怪 AngularJS1 行为,因此与控制器执行时间冲突。

我的控制器自动向下滚动到 html 锚点 ID: $anchorScroll('html_id');

这适用于静态 html (static html plunker)

<div id="hello1">Hello1 </div>

但是如果控制器在 ng-repeat 生成的 div 上调用 anchorScroll 似乎不起作用 (ng-repeat plunker)

 <div id="{{ITEM.slug}}" ng-repeat="ITEM in LIST">{{ITEM.slug}}</div>

注意:: ng-repeat 上的 anchorScroll 使用按钮可以正常工作,但我希望它在加载时自动滚动...

(我也试过使用 ng-init= 调用滚动函数,但同样的时间问题 - 所以它找不到锚点......即使在 ng-repeat 之后编码)

有什么想法/解决方法吗?

(似乎没有任何要绑定的 ng-repeat 事件...)

为此,$anchorScroll 必须在 摘要周期后执行 ,以便实际呈现项目。

实现此目的的最简单方法是使用 $timeout 并延迟 0 秒,这会创建一个将被解决的承诺。由于 0 延迟,承诺将在 当前 摘要周期完成后处理,而不是立即处理。

testApp.controller('testCtrl', function($scope, $anchorScroll, $timeout) {
  $scope.LIST = [{
    "slug": "hello1"
  }, {
    "slug": 'hello2'
  }, {
    "slug": 'hello3'
  }];

  $timeout(function() {
    $anchorScroll('hello3');
  }, 0);
});

https://plnkr.co/edit/53hlGNig7eozlm1vqkQL?p=preview