I have the following markup:
<div class="firstView" ng-controller = "watchVideo">
<figure id = "positionVideo" ng-controller = "MenuCtrl" ng-hide = "!menu" ng-show = "displayVideo">
<load-video></load-video>
</figure>
</div>
These are the controllers:
angular.module('homePage').controller('watchVideo', ['$scope', '$location', function($scope, $location) {
$scope.displayVideo = false;
$scope.videoAvailable = function () {
$scope.displayVideo = true;
console.log("videoAvailable");
};
$scope.closeVideo = function() {
$scope.displayVideo = false;
console.log("closeVideo");
};
$scope.playIt = function() {
if (jQuery("#playVideo").get(0).paused) {
jQuery("#playVideo").get(0).play();
}
else {
jQuery("#playVideo").get(0).pause();
}
}
}])
.controller('MenuCtrl', ['$scope', '$location', '$rootScope',
function($scope, $location, $rootScope) {
$scope.menu = false;
$scope.menuOpen = function() {
$scope.menu = true;
}
jQuery(".firstView").get(0).onclick = function() {
if ($scope.menu) {
console.log("window");
$scope.menu = false;
jQuery("#playVideo").get(0).pause();
$scope.$apply();
}
};
}
]);
This is the loadVideo
directive:
angular.module('homePage')
.directive('loadVideo', function($document, $window) {
return {
templateUrl: 'partials/video/video.html'
}
})
This is video.html:
<video height = "50%" width = "150%" id = "playVideo" ng-click="playIt(); menuOpen()" poster = "images/eagle.jpg" controls>
<source src = "images/lion.mp4" type = "video/mp4">
</video>
My problem is the video is being hidden when I click on it, but I want it to hide when the parent div firstView
is clicked on.
UPDATE
The problem was being caused because I was not stopping event propagation on the child. Following is the code that works perfectly.
html:
menuCtrl
was not required.
<div class="firstView" ng-controller = "watchVideo" >
<figure class="logo" ng-controller = "logo" ng-click="goToUrl('/home')"> </figure>
<cite>Every brand has a story.</cite>
<h2 id = "h2Heading"> <a ng-click = "videoAvailable()">Watch the video </a></h2>
<aside> → </aside>
<div id = "positionVideo" ng-click = "menuClose()" ng-show = "displayVideo" ng-hide = "!menu">
<load-video></load-video>
</div>
</div>
video.html:
<video ng-click="$event.stopPropagation(); playIt()" height = "75%" width = "75%" id = "playVideo" poster = "images/eagle.jpg" controls>
<source src = "images/lion.mp4" type = "video/mp4">
</video>
menuClose
in watchVideo
controller:
$scope.menuClose = function() {
$scope.menu = false;
console.log($scope.menu);
jQuery("#playVideo").get(0).pause();
}
UPDATE 2
I just noticed that I did not need to initialize the new variable menu
or create the function menuClose
either. I reused the function closeVideo
and variable displayVideo
to achieve the same functionality. I refactored my code as follows:
<div id = "positionVideo" ng-click = "closeVideo()" ng-show = "displayVideo">
<load-video></load-video>
</div>
closeVideo
in watchVideo
controller:
$scope.closeVideo = function() {
$scope.displayVideo = false;
jQuery("#playVideo").get(0).pause();
};
I changed my controller as follows:`$scope.menuClose = function() { $scope.menu = false; }` – Aug 04 '15 at 04:19