最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling AngularJS controller function when html5 video ends - Stack Overflow

programmeradmin3浏览0评论

I have a video on the homepage of my app which plays when I launch it. When the video ends I'd then like to use some CSS 3 transitions to move the page around.

<ion-view  hide-back-button="true" title="">
    <ion-pane>
        <div class="home-video">
            <video autoplay="autoplay" ng-click="ctrl.video()" aria-label="video of IBM logo" tabindex="-1" aria-hidden="true" poster="images/Hero_Final_Placeholder.gif" onended="ctrl.video()">
                <source src="videos/video.mp4" type="video/mp4">
                <img src="images/video_backup.gif" title="Your browser does not support the video tag.">
            </video>
        </div>
    </ion-pane>
</ion-view>

At the end of the video I'd like to be able to call an angularJS Controller function.

'use strict';

angular.module('app', ['ionic']).config(function ($stateProvider, $urlRouterProvider) {

$stateProvider.state('home', {
  url: "/home",
  templateUrl: 'views/home.html',
  controller: 'homeCtrl as ctrl'
}).state('project', {
  url: "/project/:projectId",
  templateUrl: 'views/project.html',
  controller: 'projectCtrl'
});

 // Default view to show
$urlRouterProvider.when('', '/home');

}).run(function ($http, pouchDB, replicationService, $rootScope) {

  replicationService.replicate();

});

My controller looks like:

'use strict';

var app = angular.module('app', false);

app.controller('homeCtrl', function ($rootScope){

this.data = {};

var ctrl = this;

    this.video = function () {

        console.log("video done");

    }

});

If I put a console.log() in the <video onended=""> element it prints it out. If I try and call ctrl.video() which contains the same console.log I get the following error:

Uncaught ReferenceError: ctrl is not defined

I know that ctrl is defined because if I add an ng-click="ctrl.video()" and I click the video player it prints out.

I have a video on the homepage of my app which plays when I launch it. When the video ends I'd then like to use some CSS 3 transitions to move the page around.

<ion-view  hide-back-button="true" title="">
    <ion-pane>
        <div class="home-video">
            <video autoplay="autoplay" ng-click="ctrl.video()" aria-label="video of IBM logo" tabindex="-1" aria-hidden="true" poster="images/Hero_Final_Placeholder.gif" onended="ctrl.video()">
                <source src="videos/video.mp4" type="video/mp4">
                <img src="images/video_backup.gif" title="Your browser does not support the video tag.">
            </video>
        </div>
    </ion-pane>
</ion-view>

At the end of the video I'd like to be able to call an angularJS Controller function.

'use strict';

angular.module('app', ['ionic']).config(function ($stateProvider, $urlRouterProvider) {

$stateProvider.state('home', {
  url: "/home",
  templateUrl: 'views/home.html',
  controller: 'homeCtrl as ctrl'
}).state('project', {
  url: "/project/:projectId",
  templateUrl: 'views/project.html',
  controller: 'projectCtrl'
});

 // Default view to show
$urlRouterProvider.when('', '/home');

}).run(function ($http, pouchDB, replicationService, $rootScope) {

  replicationService.replicate();

});

My controller looks like:

'use strict';

var app = angular.module('app', false);

app.controller('homeCtrl', function ($rootScope){

this.data = {};

var ctrl = this;

    this.video = function () {

        console.log("video done");

    }

});

If I put a console.log() in the <video onended=""> element it prints it out. If I try and call ctrl.video() which contains the same console.log I get the following error:

Uncaught ReferenceError: ctrl is not defined

I know that ctrl is defined because if I add an ng-click="ctrl.video()" and I click the video player it prints out.

Share Improve this question asked Mar 10, 2015 at 12:17 Sam MarlandSam Marland 5823 silver badges24 bronze badges 2
  • Did you find out what was the real issue/. – Asim K T Commented Jul 15, 2016 at 20:57
  • Does this answer your question? Angularjs HTML5 Video onended – Betty St Commented Aug 26, 2021 at 11:55
Add a ment  | 

2 Answers 2

Reset to default 6

Eventually what I did was use Angular UI Utils which has a generic event binder. This allowed me to bind to an event that AngularJS doesn't out of the box. Using the following line ui-event="{ ended : 'ctrl.video()'}" I was able to call my function when the video ended which gave me the desired behaviour.

I'm a bit new to Angular, so I don't know the best way to fix it, but I can at least tell you why it isn't working.

ng-click="ctrl.video()" works because ctrl is defined in the controller's scope. All ng- attributes run within the controller's scope. But onended is a regular built-in HTML5 event handler attribute, so it runs in the global Javascript scope, not the controller's scope. There's no ctrl defined there.

(I'm not sure how var ctrl = this; causes ctrl to be defined in the controller's scope, but apparently it does. Still learning Angular...)

发布评论

评论列表(0)

  1. 暂无评论