javascript-AngularJS,接口未按预期更新
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-AngularJS,接口未按预期更新,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1697字,纯文字阅读大概需要3分钟。
内容图文
我有一个无序列表,当单击每个ListItem时,它将调用setActive()
<li ng-repeat="slide in data" ng-click="setActive(slide.slideIndex)">
<span class="listItemText">
<b>{{slide.slideIndex + 1}}. </b>{{slide.Title}}
</span>
</li>
setActive()是:
$scope.setActive = function(id)
{
$scope.currentIndex = id;
}
当我单击列表项时,我会更新图像:
< img id =“幻灯片” ng-src =“资源/幻灯片/幻灯片{{currentIndex 1}}.jpg” />
一切正常
无法正常工作的是,我有一个视频播放器,在某些时候我需要更新$scope.currentIndex
我认为这样会很好:
player.addEventListener("timeupdate", function()
{
currentTime = player.currentTime;
angular.forEach($scope.data, function(value, key)
{
if (currentTime >= $scope.data[key].time && currentTime <= $scope.data[key].time + 0.3 && $scope.currentIndex != key)
{
console.log("load id:" + key);
$scope.setActive(key); //Calling what works when I click a listItem
}
});
});
console.log可以在提示点工作并记录日志,但是,直到我将鼠标悬停在列表上之后,映像才会更新.此时,映像将更新为$scope.currentIndex碰巧的值.我对此完全迷失了.有任何想法吗?
解决方法:
因为要在典型的Angular执行上下文之外更新模型,所以需要使用scope.$apply(),以便Angular知道它需要运行$digest并可能更新视图.在此处查看有关$apply的更多信息:
基本上,如果将对setActive的调用包装在$apply中,则Angular将知道它需要更新视图.这应该可以解决问题:
player.addEventListener("timeupdate", function()
{
currentTime = player.currentTime;
angular.forEach($scope.data, function(value, key)
{
if (currentTime >= $scope.data[key].time && currentTime <= $scope.data[key].time + 0.3 && $scope.currentIndex != key)
{
console.log("load id:" + key);
// Wrap the call in $apply so Angular knows to call $digest
$scope.$apply(function() {
$scope.setActive(key);
});
}
});
});
内容总结
以上是互联网集市为您收集整理的javascript-AngularJS,接口未按预期更新全部内容,希望文章能够帮你解决javascript-AngularJS,接口未按预期更新所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。