我一直是想搞清楚控制HTML5视频/ YouTube视频简单的指令模式中的两路整合视频的currentTime的。AngularJS:用指令
我想要做的“角办法”,从而绑定视频的属性添加到对象模型。但是,在处理视频的“currentTime”属性时,我遇到了一些问题,因为它不断更新。
这是我走到这一步:
HTML控件:
<!--range input that both show and control $scope.currentTime -->
<input type="range" min=0 max=60 ng-model="currentTime">
<!--bind main $scope.currentTime to someVideo directive's videoCurrentTime -->
<video some-video video-current-time="currentTime"> </video>
指令:
app.controller('MainCtrl', function ($scope) {
$scope.currentTime = 0;
})
app.directive('someVideo', function ($window) {
return{
scope: {
videoCurrentTime: "=videoCurrentTime"
},
controller: function ($scope, $element) {
$scope.onTimeUpdate = function() {
$scope.videoCurrentTime = $element[0].currentTime;
$scope.$apply();
}
},
link: function (scope, elm) {
scope.$watch('videoCurrentTime', function (newVar) {
elm[0].currentTime = newVar;
});
elm.bind('timeupdate', scope.onTimeUpdate);
}
}
})
JSFiddler:http://jsfiddle.net/vQ5wQ/
::
虽然这看起来作品,请注意每次onTimeUpdate
火灾时,会触发$watch
。
例如,当视频运行到10秒时,它会通知onTimeUpdate将模型更改为10,$ watch将捕获此更改并要求视频再次寻找10秒。
这段时间创建了一个循环,导致视频滞后于时间。
你认为有更好的方法来做到这一点?一种不会触发不需要的$ watch的方法?任何建议表示赞赏。
'currentTime'的分辨率是多少?我知道'ontimeupdate'每运行一帧视频(每秒多次) - 'currentTime'获得亚秒精度吗?如果是这样,在'$ watch'内部,您可以在进行更改之前检查'newVar'和'elem [0] .currentTime'之间的最小差异。 – jkjustjoshing