I have just started my first angular.js project and have a situation where a user clicks a button which sets a value on my model but what I also need now is the the html video element to have it's currentTime property set to a value and the video that is currently playing to pause.
So what I need is
1) Update my model (which I have done via ng-click)
2) Run some javascript which does
{
$('videoElement')[0].currentTime = time (from my model);
$('videoElement')[0].pause();
}
I am not sure where this second step should be triggered from and how?
You should use a directive to do DOM manipulation. Hopefully your videoElement is in the same scope as your button. If so, define a directive that $watch()es your model:
angular.module('myModule', [])
.directive('pauseVideo', function() {
return {
scope: { someProperty: '@pauseVideo' },
link: function(scope, element, attrs) {
scope.$watch('someProperty', function(value) {
var videoElement = $('videoElement')[0];
videoElement.currentTime = time(value);
videoElement.pause();
})
}
}
})
Add the scope property to the HTML element where videoElement is used. E.g., if the property you manipulate with ng-click is $scope.someProperty:
<div id="videoElement" pause-video="someProperty">
<a ng-click="pauseVideo()">pause video</a>
In your controller:
$scope.pauseVideo = function() {
$scope.someProperty = ...;
var videoElement = $('videoElement')[0];
videoElement.currentTime = time (from my model);
videoElement.pause();
}