JavaScript Custom directives and routing in AngularJS

I'm quite new to AngularJS and Javascript as well, but I have managed to put together a custom camera directive that works the way I want it to. It let me choose camera from a list of available cameras, take snapshots, shows the picture taken and has buttons for saving a picture or dismiss it. The problem is that when I add routing to the app nothing happens any more when clicking the buttons. So my question is how do I get this directive to work as before when using routing with the app?

Here is the directive:

app.directive('camera', function () {
return {
    template: '\
    <div ng-show="showVideo">\
        <div class="select">\
            <label for="videoSource">Video source: </label><select id="videoSource"></select>\
        </div>\
        <video id="video" width="640" height="480" autoplay></video>\
        <button id="snap">Snap Photo</button>\
    </div>\
    <div ng-show="showSnap">\
        <canvas id="canvas" width="640" height="480"></canvas>\
        <button id="save">Save Photo</button>\
        <button id="cancel">Cancel</button>\
    </div>',
    link: function ($scope) {

        $scope.showVideo = true;
        $scope.showSnap = false;

        var videoElement = document.querySelector('video');
        var videoSelect = document.querySelector('select#videoSource');

        navigator.getUserMedia = navigator.getUserMedia ||
            navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

        function gotSources(sourceInfos) {
            for (var i = 0; i !== sourceInfos.length; ++i) {
                var sourceInfo = sourceInfos[i];
                var option = document.createElement('option');
                option.value = sourceInfo.id;
                if (sourceInfo.kind === 'video') {
                    option.text = sourceInfo.label || 'camera ' + (videoSelect.length + 1);
                    videoSelect.appendChild(option);
                } else {
                    console.log('Some other kind of source: ', sourceInfo);
                }
            }
        }

        if (typeof MediaStreamTrack === 'undefined') {
            alert('This browser does not support MediaStreamTrack.\n\nTry Chrome Canary.');
        } else {
            MediaStreamTrack.getSources(gotSources);
        }

        function successCallback(stream) {
            window.stream = stream; // make stream available to console
            videoElement.src = window.URL.createObjectURL(stream);
            videoElement.play();
        }

        function errorCallback(error){
            console.log('navigator.getUserMedia error: ', error);
        }

        function start(){
            if (!!window.stream) {
                videoElement.src = null;
                window.stream.stop();
            }
            var videoSource = videoSelect.value;
            var constraints = {
                audio: false,
                video: {
                    optional: [{sourceId: videoSource}]
                }
            };
            navigator.getUserMedia(constraints, successCallback, errorCallback);
        }

        videoSelect.onchange = start;

        start();

        // Take snapshot example

        // Put event listeners into place
        window.addEventListener("DOMContentLoaded", function() {
            // Grab elements, create settings, etc.
            var canvas = document.getElementById("canvas"),
            context = canvas.getContext("2d"),
            video = document.getElementById("video"),
            videoObj = { "video": true },
            errBack = function(error) {
                console.log("Video capture error: ", error.code); 
            };

        // Trigger photo take
        document.getElementById("snap").addEventListener("click", function() {
            $scope.$apply(function () {
                context.drawImage(video, 0, 0, 640, 480);
                $scope.showVideo = false;
                $scope.showSnap = true;
            });
        });

        // Save photo
        document.getElementById("save").addEventListener("click", function() {
            $scope.$apply(function () {
                var image = new Image();
                image.src = canvas.toDataURL("image/jpg");
                $scope.image = image.src;
                console.log($scope.image);
                $scope.showVideo = true;
                $scope.showSnap = false;
                //return image;
            });

        });
        document.getElementById("cancel").addEventListener("click", function() {
            $scope.$apply(function () {
                $scope.showVideo = true;
                $scope.showSnap = false;
            });
        });
    }, false);
    }
};
});

Here is my app.js:

var app = angular.module('cameraApp', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
         $routeProvider .when('/', {
             controller: 'CameraController',
             templateUrl: 'views/app.html'
         })
         .when('/login', {
             controller: 'LoginController',
             templateUrl: 'views/login.html'
         })
         .otherwise({redirectTo: '/login'});
}]);


app.controller('MainController', function($scope, $window) {
    console.log($window);
    console.log($scope);
});

app.controller('LoginController', function($scope) {

});

app.controller('CameraController', function($scope) {

});

And here is my index.html:

<!DOCTYPE html>
<html lang="en" ng-app="cameraApp">
<head>
    <meta charset="utf-8">
    <title>AngularCameraApp</title>
    <!--<link rel="stylesheet" target='_blank' href="style.css">-->
</head>
<body ng-controller="MainController">

    <ul>
        <li><a target='_blank' href="#/login">Login</a></li>
        <li><a target='_blank' href="#/">App</a></li>
    </ul>

    <div ng-view></div>

    <script src="js/lib/angular.min.js"></script>
    <script src="js/lib/angular-route.min.js"></script>
    <script src="js/app.js"></script>
    <script src="js/directives.js"></script>
</body>

Any help with this is much appreciated!

Answer:1

When one column has a long value, without spaces, the table breaks out of any container with a set width (or percentage). Currently, I'm needing a table with 50% width, and the long value breaks the ...

When one column has a long value, without spaces, the table breaks out of any container with a set width (or percentage). Currently, I'm needing a table with 50% width, and the long value breaks the ...

  1. for long columns the value of buckling load is
  2. column_value long
  3. column_value long example

I have a property grid with multiple comboboxes and I want to filter the values in one box based on what is selected in the previous box. My code looks like this: var tempPropGrid = me.getView().add(...

I have a property grid with multiple comboboxes and I want to filter the values in one box based on what is selected in the previous box. My code looks like this: var tempPropGrid = me.getView().add(...

  1. extjs filter combobox
  2. extjs filter combobox store
  3. extjs combobox filter as you type
  4. extjs combobox filter store example
  5. extjs combobox filter anymatch
  6. extjs combobox filter not working
  7. extjs combobox filter example
  8. extjs grid filter combobox
  9. extjs 3.4 combobox filter store
  10. extjs 6 combobox filter
  11. extjs 4 grid filter combobox
  12. extjs combobox filter event

My angular is 1.0.8-stable My objective is to display data in rows of 3. My html should look like <div class="table-row"> <div class="item">item1</div> <div class="item"...

My angular is 1.0.8-stable My objective is to display data in rows of 3. My html should look like <div class="table-row"> <div class="item">item1</div> <div class="item"...

I just came across the following line of code in one of the Windows Store Apps samples. if (that.assets[asset].object === null || !!!that.assets[asset].object.canPlayType) { It uses a triple ...

I just came across the following line of code in one of the Windows Store Apps samples. if (that.assets[asset].object === null || !!!that.assets[asset].object.canPlayType) { It uses a triple ...

  1. what does ���� mean
  2. what does smh mean
  3. what does boomer mean
  4. what does ok boomer mean
  5. what does the fox say
  6. what does simp mean
  7. what does sos mean
  8. what does dna stand for
  9. what does noel mean
  10. what does asl mean
  11. what does ig mean
  12. what does gap stand for
  13. what does otp mean
  14. what does uwu mean
  15. what does mean
  16. what does goat mean
  17. what does bomboclaat mean
  18. what does pansexual mean
  19. what does woke mean
  20. what does ion mean