簡體   English   中英

角范圍數據在更新時不綁定到視圖,但是在初始化時綁定

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

我有一個角度視圖,可以在添加數據時將其正確綁定到示波器中的相應數據。 嘗試刪除或更新此數據時,它沒有綁定到視圖。 以下是正在觸發的功能:

該視圖綁定到兩個數據:$ 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中的數據是正確的(並且此函數在其第一次調用期間將數據正確綁定到了視圖)...但視圖並未已通過此調用更新。

PM 77-1和TheSharpieOne抓住了我的業余失誤。 必須調用$ scope。$ apply來更新視圖中的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM