角度范围数据在更新时不绑定到视图,但在初始化时绑定

时间:2014-09-22 21:48:44

标签: javascript angularjs data-binding scope

我有一个角度视图,在添加数据时正确绑定到范围内的相应数据。尝试删除或更新此数据时,它不会绑定到视图。以下是被触发的功能:

该视图绑定了两个数据:$ scope.music_stream.currently_playing_track& $ scope.music_stream.queued_tracks。

所以最初当用户添加视频时,会调用此函数并在视图中正确显示。

    /**
     * Create an object representing the data needed for the currently playing track
     * @param {Number} id
     * @param {String} thumbnail
     * @param {String} title
     * @param {String} added_by
     * @param {String} track_duration
     */
    $scope.createPlayingTrack = function(id, thumbnail, title, added_by, track_duration) {
        var currently_playing_track_object = {
            id : id,
            thumbnail : thumbnail,
            title : title,
            added_by : added_by,
            vote_value : 0,
            track_duration : '',
            elapsed_time : '0:00'
        };

        $scope.music_stream.currently_playing_track = currently_playing_track_object;
        $scope.play($scope.music_stream.currently_playing_track.id);

        currently_playing_track.track_duration = player.getDuration();
    }

稍后当这个视频结束时,这个函数被称为

    /**
     * Triggered when the player reaches the end of a video
     */
    $scope.loadNextVideo = function() {
        if(!jQuery.isEmptyObject($scope.music_stream.queued_tracks[0])) { // if something is in the queue
            next_video = $scope.music_stream.queued_tracks.shift();
            $scope.createPlayingTrack(next_video.id, '/assets/album_cover1.jpg', next_video.title, 'ah', '3:00')
        }
    }

当你在shift()之后调用console.log($ scope.music_stream.queued_tracks)时,会返回[](这就是我想要的),但这不会转移到视图中。

更令人困惑的部分......当再次调用createPlayingTrack()时,$ scope.music_stream.currently_playing_track中的数据是正确的(并且此函数在第一次调用期间正确地将数据绑定到视图中)...但是视图没有使用此调用进行更新。

1 个答案:

答案 0 :(得分:0)

PM 77-1和TheSharpieOne抓住了我的业余错误。必须调用$ scope。$ apply来更新视图中的数据。