简体   繁体   English

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

[英]Angular scope data not binding to view when updated, but is binding when initialized

I have an angular view that correctly binds to its corresponding data in the scope when data is added. 我有一个角度视图,可以在添加数据时将其正确绑定到示波器中的相应数据。 When trying to remove or update this data, it is not binding to the view. 尝试删除或更新此数据时,它没有绑定到视图。 Here are the functions that are being triggered: 以下是正在触发的功能:

the view is bound to two pieces of data: $scope.music_stream.currently_playing_track & $scope.music_stream.queued_tracks. 该视图绑定到两个数据:$ scope.music_stream.currently_playing_track和$ scope.music_stream.queued_tracks。

so initially when a user adds a video, this function is called and is correctly displayed in the view. 因此,最初,当用户添加视频时,此函数将被调用并正确显示在视图中。

    /**
     * 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();
    }

later on when this video ends, this function is called 稍后,当该视频结束时,将调用此函数

    /**
     * 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')
        }
    }

when you console.log($scope.music_stream.queued_tracks) after the shift(), [] is returned (which is what I want), but this is not carried over to the view. 当您在shift()之后执行console.log($ scope.music_stream.queued_tracks)时,将返回[](这是我想要的),但是这并不会传递到视图中。

The more confusing part... when createPlayingTrack() is called again, the data in $scope.music_stream.currently_playing_track is correct (and this function correctly binded data to the view during its first call) ... but the view is not being updated with this call. 更加令人困惑的部分...当再次调用createPlayingTrack()时,$ scope.music_stream.currently_playing_track中的数据是正确的(并且此函数在其第一次调用期间将数据正确绑定到了视图)...但视图并未已通过此调用更新。

PM 77-1 and TheSharpieOne caught my amateur mistake. PM 77-1和TheSharpieOne抓住了我的业余失误。 $scope.$apply must be called to update data in the view. 必须调用$ scope。$ apply来更新视图中的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM