简体   繁体   English

Ionic:AngularJS 变量不使用 $scope 更新 DOM

[英]Ionic: AngularJS Variables not Updating DOM with $scope

I've got an object that is dynamically updated to represent the currently playing track in my Ionic app, such that the visual feedback is the currently playing track art and title.我有一个动态更新的对象来表示我的 Ionic 应用程序中当前正在播放的曲目,这样视觉反馈就是当前正在播放的曲目艺术和标题。 This function works absolutely fine and I know it updates correctly because the interface is updated every time I press a button to skip to the next track.这个功能工作得很好,我知道它会正确更新,因为每次我按下按钮跳到下一首曲目时界面都会更新。

However, I've added in a setInterval() so that tracks can advance automatically and now the interface does not update each time a track advances.但是,我添加了setInterval()以便轨道可以自动前进,现在界面不会在每次轨道前进时更新。 The variables are updating in the $scope correctly, because I can hear the correct tracks playing, but interface elements do not update to match.变量在$scope正确更新,因为我可以听到正确的曲目播放,但界面元素不会更新以匹配。

Is this a DOM problem?这是 DOM 问题吗? Does Ionic do this automatically on button presses? Ionic 是否会在按下按钮时自动执行此操作? I just don't understand how the same code wouldn't work when running on a timer instead of requiring user input.我只是不明白在计时器上运行而不是需要用户输入时,相同的代码如何不起作用。

JS: JS:

function advance() {
        var i = currentIndex;

        $scope.stop();
        if (i < 4) {
            $scope.current = {
                'index': i + 1,
                'track': $scope.active.tracks[i + 1]
            };
            $scope.upNext = $scope.active.tracks[i + 2];
            $scope.play($scope.current.track.preview);

            currentIndex++;

            return;
        }

        $scope.closeModal();
    }

I'm sorry if this question might be a tad vague, I can expand if need be.如果这个问题可能有点含糊,我很抱歉,如果需要,我可以扩展。

Edit: The advance() function is called by this function:编辑:该函数调用advance()函数:

$scope.open = function (i, r) {
        console.log(currentIndex);
        $scope.openModal(i, r);

        autoAdvance = window.setInterval(advance, 30000);
    };

Like I say, the advance function works and the $scope variables are being updated;就像我说的,advance 函数有效并且 $scope 变量正在更新; it is just not reflected in the HTML as it updates.它只是在更新时不会反映在 HTML 中。

So, I believe what is occurring is that that the interval isn't passing through angular digest cycle, so it's unaware of the changes to the scope.所以,我相信正在发生的事情是间隔没有通过角度消化周期,所以它不知道范围的变化。 Whereas, a button click is called from within angular itself, so it's aware of those changes.而按钮单击是从 angular 内部调用的,因此它知道这些更改。

Calling $scope.$apply() informs angular of these changes and kickstarts the engine to render the changes.调用 $scope.$apply() 将这些更改通知 angular 并启动引擎以呈现更改。 Take the following controller:采取以下控制器:

 function MyCtrl($scope) { $scope.name = 'Superhero'; window.setInterval(function() { advance() }, 2000); function advance() { $scope.name += " 1"; } window.setInterval(function() { advance2() }, 2000); function advance2() { $scope.name += " 2"; $scope.$apply() } }

advance1() has no $scope.$apply() call, while the advance2() does. Advance1() 没有 $scope.$apply() 调用,而 Advance2() 有。 You'll notice that both function calls are reflected in the DOM after the second call.您会注意到,在第二次调用后,两个函数调用都反映在 DOM 中。

Here's a fiddle showing the functionality: http://jsfiddle.net/pgxnom55/这是显示功能的小提琴: http : //jsfiddle.net/pgxnom55/

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

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