2013-02-26 83 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