0

我正在编写一个非常简单的 AngularJS 指令来与 Spotify API (1.0) 进行通信。我有这个应该创建 SpotifyList视图的自定义指令(在此处参考:https ://developer.spotify.com/docs/apps/views/1.0/list-list.html )。

这是 Angular 指令:

angular.module('spotify.view.list', [])
  .directive('spList', function($rootScope, $timeout) {
    var rootScope = $rootScope
    return {
      restrict: 'EA',
      replace: true,
      link: function($scope, el, attrs) {
        var fn = attrs['for'], list = null, playlist
        attrs.$observe('uri', function(newval, oldval) {
          require(['$views/list#List', '$api/models'], function(List, models) {   
            if(newval != oldval && list == null) {
              playlist = models.Playlist.fromURI(attrs.uri)
              list = List.forPlaylist(playlist, {
                layout: 'default',
                header: attrs.header||'no',
                fields: attrs.fields.split(','),
                height: attrs.height||'fixed',
                numItems: 10
              })

              var targetEl = document.getElementById(attrs.node)
              targetEl.appendChild(list.node)
              list.init()

            }
          })
        })

      }
    }
  })

这就是我在 Angular 模板中调用指令的方式:

    <div class="playlist-tracklist" id="sp-playlist"></div>

    <sp-list
        for="playlist"
        fields="track,artist"
        header="yes"
        type="tracks"
        layout="default"
        uri="{{playlist.playlist_uri}}"
        node="sp-playlist">
    </sp-list>

这里的问题是我随机得到一个DOMException Error

Uncaught Error: NotFoundError: DOM Exception 8

这完全是随机的:有时它可以工作,我会返回一个 Spotify 列表,有时它不会,并且控制台不会给我任何其他调试信息。

我找不到任何方法来重现异常,但我想这与targetEl.

我很想知道为什么尝试将列表附加到 abritrary HTML 元素会失败,即使这个 HTML 元素确实存在,以及异常的原因是什么。

4

1 回答 1

1

I think you may be re-inserting script tags each time you call require; the library does not do any caching of those modules. I believe the DOM exception happens when a script tag times out for some reason (server does not return a 304 or 200). Try loading the modules outside of your angular directive and also can you try and verify if it is actually loading script tags more than necessary?

于 2013-09-04T23:05:00.083 回答