我对 AngularJS 和指令的一个非常基本的示例有疑问。我想创建一个使用 webrtc 显示网络摄像头图像的指令。我的代码完美地显示了流,但是如果我添加超时(例如刷新画布) $timeout 不起作用,这是代码:
wtffDirectives.directive('scannerGun',function($timeout){
return {
restrict: 'E',
template: '<div>' +
'<video ng-hide="videoStatus"></video>' +
'<canvas id="canvas-source"></canvas>' +
'</div>',
replace: true,
transclude: true,
scope: false,
link: function postLink($scope, element){
$scope.canvasStatus = true;
$scope.videoStatus = false;
width = element.width = 320;
height = element.height = 0;
/* this method draw the webcam image into a canvas */
var drawVideoCanvas = function(){
sourceContext.drawImage(vid,0,0, vid.width, vid.height);
};
/* start the timeout that take a screenshot and draw the source canvas */
var update = function(){
var timeout = $timeout(function(){
console.log("pass"); //the console log show only one "pass"
//drawVideoCanvas();
}, 2000);
};
/* this work perfectly and reproduct into the video tag the webcam */
var onSuccess = function onSuccess(stream) {
// Firefox supports a src object
if (navigator.mozGetUserMedia) {
vid.mozSrcObject = stream;
} else {
var vendorURL = window.URL || window.webkitURL;
vid.src = vendorURL.createObjectURL(stream);
}
/* Start playing the video to show the stream from the webcam*/
vid.play();
update();
};
var onFailure = function onFailure(err) {
if (console && console.log) {
console.log('The following error occured: ', err);
}
return;
};
var vid = element.find('video')[0];
var sourceCanvas = element.find('canvas')[0];
var sourceContext = sourceCanvas.getContext('2d');
height = (vid.videoHeight / ((vid.videoWidth/width))) || 250;
vid.setAttribute('width', width);
vid.setAttribute('height', height);
navigator.getMedia (
// ask only for video
{
video: true,
audio: false
},
onSuccess,
onFailure
);
}
}
});
问题是什么?为什么 $timeout 在这种情况下不起作用?终于有解决方案了吗?
提前致谢