2013-02-26 47 views
0

所以当我点击教程导航菜单时,它触发了ng-click =“loadNewTutorial($ index)”函数,一切正常。tutorialNumber被索引号更新并且依次更改当前视频的网址。唯一的问题是video.load()/ video.play()显然会在DOM发生变化之前触发,因为视频始终是一次点击。我的意思是我可以点击菜单项来加载第二个教程视频,但它会加载第一个,然后下一次我点击它会加载第二个,然后如果我再次点击第一个,它会卡在第二个直到我再次点击第一个。所以目前的视频并没有及时加载正确的网址,并且一直点击一下!如何使video.load()函数等待,直到DOM中的视频src属性在触发之前被更改?等待DOM src属性改变

<!DOCTYPE html> 
<html ng-app="Tutorials"> 
    <head> 
     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script> 
     <script src="./functions.js"></script> 
     <script src="./tutorials.js"></script> 
     <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
     <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script> 
     <script type="text/javascript" src="./script-ng.js"></script> 
     <link rel="stylesheet" href="main.css"></link> 
    </head> 
    <body ng-controller="getAnswers"> 
     <div id="sidebar_left"><div id="header"></div> 
      <ul id="nav" ng-repeat="section in sets"> 
       <li class="section_title {{section.active}}" > 
        {{section.name}} 
       </li> 
       <ul><li class="tutorial_title {{tutorial.active}}" ng-click="loadNewTutorial({{$index}})" ng-repeat="tutorial in section.tutorials">{{tutorial.name}}</li></ul> 
      </ul><span>{{score}}</span> 
      </div> 
     <div id="container"> 
       <video id="video" controls> 
        <source src="{{videoURLs[tutorialNumber]}}.mp4"></source> 
        <source src="{{videoURLs[tutorialNumber]}}.webm"></source> 
        Your browser does not support the video tag. 
       </video> 
       <br /> 
       <button id="reset">Replay</button> 
      <br /> 
      <div id="pannel"> 
       <div id="base">{{verbs.base}}</div> 
       <ul class="answers"> 
        <li ng-click="checkAnswer(answer)" ng-repeat="answer in verbs.conjugations" class="{{answer.colorReveal}} answer {{answer.correct}}" id="answer{{$index}}">{{answer.text}}</li> 
       </ul> 
      </div> 
      <br /> 
      <br /> 
      <br /> 
      <div id="warning"></div> 
     </div> 
    </body> 
</html> 



angular.module('Tutorials', ['functions', 'tutorials']).controller('getAnswers', function ($scope, $element) { 
    $scope.sectionNumber = 0; 
    $scope.tutorialNumber = 0;  
      $scope.loadNewVerbs = function() { 
      $scope.verbs = conjugate(conjugationsets[$scope.tutorialNumber][$scope.questionNumber]); 
      $scope.correct = $scope.verbs.conjugations[0].text; 
      fisherYates($scope.verbs.conjugations); 
     }; 
    $scope.checkAnswer = function (answer) { 
     if (false) {//wait for tutorial to pause 
      $("#warning").text("Not yet!").fadeIn(300).delay(1400).fadeOut(300); 
      return; 
     }; 
     answer.colorReveal = "reveal-color"; 
     if (answer.text === $scope.correct) { //if correct skip to congratulations    
      $scope.questionNumber++; 
     if(sectionNumber === 0 && $scope.tutorialNumber ===0){ //if first video play congratulations etc 
      congrats(); 
     } 
     setTimeout(function() { 
      $scope.loadNewVerbs(); 
      $scope.$digest(); 
     }, 2000); 
     } else { //if incorrect skip to try again msg 
      if(sectionNumber === 0 && $scope.tutorialNumber ===0){start(160.5); pause(163.8)} 
     } 
    }; 
    $scope.sets = [{ 
     active:"inactive", 
     name: "Conjugation", 
     tutorials: [ 
      {active:"inactive",name: "ㅗ and ㅏ regular"}, 
      {active:"inactive",name:"ㅜ, ㅓ and ㅣ regular"}, 
      {active:"inactive",name:"ㅏ and ㅓ reductive"}, 
      {active:"inactive",name: "ㅗ and ㅜ reductive"}, 
      {active:"inactive",name: "ㅣ reductive"}] 
    }, { 
     active:"inactive", 
     name: "Sentence_Building", 
     tutorials: [ 
      {active:"inactive",name:"Particles"}, 
      {active:"inactive",name:"Word Order"}] 
    }]; 
    $scope.video = $("#video").get(0); 
    $scope.videoURLs = ["ㅗㅏregular", "ㅜㅓㅣregular"]; 

    $scope.loadNewTutorial = function (tut) { 
     $scope.score = 0; 
     $scope.questionNumber = 0; 
     if(tut){ 
      $scope.tutorialNumber = tut; 
     } 
     video.load(); 
     video.play(); 
     video.hasPlayed = true; 
     $(video).bind('ended', endVideoHandler); 

     $scope.loadNewVerbs(); 
    }; 

    $scope.loadNewTutorial(); 

    var congrats = function(){ 
     if ($scope.questionNumber === 1) {start(164.15); pause(166.8); $scope.score++; 
     } else if ($scope.questionNumber === 2) {start(167.1); pause(170); $scope.score++; 
     } else if ($scope.questionNumber === 3) { 
      start(171.1); $scope.score ++;$scope.questionNumber = 0;$scope.tutorialNumber++; 
     } 
    } 

    function endVideoHandler() { 
     $(this).unbind('ended'); 
     if (!this.hasPlayed) { 
      return; 
     } 
     $scope.tutorialNumber++; 
     $scope.$digest(); 
     this.currentTime = 0; 
     $scope.loadNewTutorial(); 
    } 
}); 

var conjugationsets = [ 
    ["작다", "놀다", "닦다"], 
    ["웃다", "울다", "멀다"] 
]; 
var sectionNumber = 0; 
var tutorialNumber = 0; 
var questionNumber = 0; 


$(function() { 

    $("#nav").accordion({ 
     heightStyle: "content", 
     header: ".section_title" 
    }); 


    $("#reset").click(function() { 
     currentTutorial.currentVideo.videoObject.currentTime = 0; 
     currentTutorial.currentVideo.start(); 
    }); 

}); 

回答

0

编辑: 我不知道我理解你的代码在这里,但是为什么loadNewTutorial采取$范围为变元时,它已经可用?我在猜测,这是你的问题的一部分,因为你无法将范围从模板传递给它。

原始: “loadNewTutorial({{$ index}})”应该是“loadNewTutorial($ index)”。

+0

对不起,请参阅编辑,我是愚蠢的 – Tules 2013-02-26 19:35:25

+0

通过查看stewies的答案解决了这个问题,现在有URL问题,请参阅编辑 – Tules 2013-02-26 20:36:26

0

我看到几个问题,你的“loadNewTutorial”功能:当它不应该(该功能在$范围内定义,以使$范围

  1. 它走的是一条$范围参数作为参数该功能默认可用)。

  2. 由于它是第一个参数,预计会收到一个$ scope,但是在一个地方你用当前范围调用它,在其他地方你没有任何参数调用它,然后在视图中传递它时调用它$ index(它解析为一个原始数字),因为它是第一个参数。

  3. 它每次调用时都重新定义'loadNewVerbs'和'checkAnswer'函数。

仅举几例。

+0

感谢您的建议我已经做出了这些更改(请参阅编辑),但我仍然有问题与视频src attr/video.load()问题 – Tules 2013-02-26 20:12:03

+0

几个技巧:1)尝试通过评论/删除所有你认为与问题没有直接关系的问题来隔离你的问题。 2)尝试并在jsFiddle的Plunker中重现问题并共享链接。 3)试着将你的视频逻辑嵌入到指令中,这样你就可以从直接的jQuery操作中清除你的控制器(例如'_ $(video).bind_')。如果你这样做,你会帮助我们帮助你,作为奖励,你可能会自己找到解决方案。 – Stewie 2013-02-26 20:42:16

+0

好吧我会尝试在视频中嵌入视频逻辑,这里是一个链接,而不是一个jsFiddle http://koreanwordgame.com/grammar/grammar-ng.html – Tules 2013-02-26 20:55:21