callback function in AngularJS -
i m trying assign value variable in callback function. how know callback functions asynchronous tried scope.apply doesnt seems work.. ideas ?
angular.module("sadf") .factory("browserscamerasupportservice", function ($scope, $apply ) { return { supportsgetusermedia: function () { navigator.getusermedia = navigator.getusermedia || navigator.webkitgetusermedia || navigator.mozgetusermedia || navigator.msgetusermedia; var hascam = true; if (navigator.getusermedia) { navigator.getusermedia({video: true}, function(localmediastream){ }, function(){ $scope.$apply(function(){ hascam = false; }); }); } return angular.isdefined(navigator.getusermedia)&& hascam; } }; });
this case using $apply not need. since check media asynchronous operation convenient return promise object , work in consuming code. service become:
angular.module("sadf") .factory("browserscamerasupportservice", function($q) { return { supportsgetusermedia: function() { navigator.getusermedia = navigator.getusermedia || navigator.webkitgetusermedia || navigator.mozgetusermedia || navigator.msgetusermedia; return $q(function(resolve, reject) { if (navigator.getusermedia) { navigator.getusermedia({video: true}, function(localmediastream) { return resolve(true); }, function() { return resolve(false); }); } }); } }; });
and usage simple:
browserscamerasupportservice.supportsgetusermedia().then(function(supported) { if (!supported) { alert('video not supported.'); } });
Comments
Post a Comment