ngRepeat with $scope.$apply 在指令中创建重复项

ngRepeat with $scope.$apply creates duplicates in directive

我有一个指令可以通过模板将动态数据放入 qTip。它通过获取模板并在其上使用 $compile(请原谅 coffeescript)来实现:

$http.get scope.qtipTemplate, cache: $templateCache
.then (html) ->
  clone = $compile html.data
  generateQtip text: ->
    scope.$apply ->
      clone scope

generateQtip 只是在指令元素上创建一个新的 qTip,并将第一个参数作为 content 属性 放在选项对象上。

不过,每次我打开 qTip 时,模板中的 ngRepeat 都会生成重复的列表,即使使用 limitTo 作为过滤器也是如此。示例代码:

<ul>
    <li ng-repeat="person in object.people | limitTo:3 track by $index">{{person.name}}</li>
</ul>

在我第一次打开 qTip 时生成以下内容:

  • John Doe
  • Jane Doe
  • Johnny Dowy

并且在第 时间:

  • John Doe
  • Jane Doe
  • Johnny Dowy
  • John Doe
  • Jane Doe
  • Johnny Dowy

这是第三次时间:

  • John Doe
  • Jane Doe
  • Johnny Dowy
  • John Doe
  • Jane Doe
  • Johnny Dowy
  • John Doe
  • Jane Doe
  • Johnny Dowy

以此类推,以此类推。

为什么会这样?有任何想法吗?这是我在 this link.

中的完整指令代码

不知道为什么,但我不得不将 $compile 移到更里面。

$http.get scope.qtipTemplate, cache: $templateCache
.then (html) ->
  generateQtip text: ->
    scope.$apply ->
      $compile(html.data)(scope)