我對AngularJS和指令非常基本的例子有問題。 我想創建一個指令,用webrtc顯示攝像頭圖像。 我的代碼顯示流完美,但如果我添加超時(例如刷新畫布)在$超時不工作 這是代碼:
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
);
}
}
});
問題是什麼?爲什麼$超時在這種情況下不起作用?終於有了解決辦法?
感謝的提前
UFFF抱歉浪費你的時間.... 這是正確的! 祝你有美好的一天 – cingusoft
如果你使用RC版本(1.2.x),考慮使用'$ interval'而不是'setInterval'和'$ scope。$ apply'。 '$ interval'會關心你的DOM刷新。 –