2017-04-20 35 views
0

我有一個內置於AngularJS中的測驗應用程序,我想在兩個對象之間進行選擇,具體取決於您在視圖中選擇哪個測驗。在視圖中選擇測驗時,將執行一個功能。在這個過程中,我向包含對象的dataservice.js文件發送一個變量,在這裏我有一個if語句,根據來自控制器的輸入選擇正確的對象。 如何在changeState函數外使用測驗變量?我想用它來選擇兩個測驗對象。將價值從控制器傳遞到工廠以在對象之間進行選擇

//控制器

function activateQuiz(){ 

     quizMetrics.changeState("quiz", true); 
     DataService.startQuiz("quiz_1"); 
     //test("hund"); 

    } 

    function activateQuiz_2(){ 

     quizMetrics.changeState("quiz", true); 
     DataService.startQuiz("quiz_2"); 
     //test("kat"); 

    } 

//廠

(函數(){

/* 
* Declaring a factory service as part of the existing turtleFacts Module. 
*/ 
angular 
    .module("turtleFacts") 
    .factory("DataService", DataService); 
/* 
* Actual definition of the function used for this factory 
*/ 
function DataService(){ 
    /* 
    * dataObj is used to simulate getting the data from a backend server 
    * The object will hold data which will then be returned to the other 
    * factory declared in js/factory/quiz.js which has this factory 
    * as a dependency 
    */ 

    var dataObj = { 
     startQuiz: startQuiz, 
     quizQuestions: quizQuestions, 
     correctAnswers: correctAnswers 
    }; 

    var dataObj_2 = { 
     startQuiz: startQuiz, 
     quizQuestions: quizQuestions_2, 
     correctAnswers: correctAnswers_2 
    }; 


      var quiz; 
      function startQuiz(metric, state){ 
      if(metric === "quiz_1"){ 
       quiz = "quiz1" 
      }else if(metric === "quiz_2"){ 
       quiz = "quiz2" 
      }else{ 
       return false; 
      } 
      console.log(quiz) 

     } 

      if (quiz = "quiz1") { // here i want to use the the quiz variable from the changeState function. 
      return dataObj; 
       } else if (quiz = "quiz2") { 
      return dataObj_2; 
      } 

} 

回答

0

你可能有這樣的事情:

/* 
* Declaring a factory service as part of the existing turtleFacts Module. 
*/ 
angular 
    .module("turtleFacts") 
    .factory("DataService", DataService); 
/* 
* Actual definition of the function used for this factory 
*/ 
function DataService(){ 
    /* 
    * dataObj is used to simulate getting the data from a backend server 
    * The object will hold data which will then be returned to the other 
    * factory declared in js/factory/quiz.js which has this factory 
    * as a dependency 
    */ 

    var dataObj = { 
     changeState: changeState, 
     quizQuestions: quizQuestions, 
     correctAnswers: correctAnswers 
    }; 

    var dataObj_2 = { 
     changeState: changeState, 
     quizQuestions: quizQuestions_2, 
     correctAnswers: correctAnswers_2 
    }; 

    var quiz; 

    function changeState(metric, state){ 
     if(metric === "quiz_1"){ 
      quiz = "quiz1" 
     }else if(metric === "quiz_2"){ 
      quiz = "quiz2" 
     }else{ 
      return false; 
     } 
     console.log(quiz); 

     if (quiz == "quiz1") { // here i want to use the the quiz variable from the changeState function. 
      return dataObj; 
     } else if (quiz == "quiz2") { 
      return dataObj_2; 
     } 
    } 
} 

從你控制器,你可以得到數據a s:

$scope.quiz = DataService. changeState(metric, state); 
+0

嗨Pratek感謝您的答案。我無法讓它工作。我正在嘗試使用rootcope傳遞一個值,直到我可以使其與服務一起工作。 – Hermansson

+0

你是什麼意思*無法得到它的工作*?有錯誤嗎?你有不同的迴應嗎?你能否詳細說明這個問題? –

+0

嗨Prera​​k。我能夠將一個值從控制器傳遞到我已經放在工廠數據服務功能中的變更狀態功能中。測驗變量取決於在html頁面上選擇哪個測驗,所以這個工作正常。但我似乎無法在條件中選擇測驗1或測驗2中的測驗變量。我在這裏做錯了什麼。感謝您的幫助。 /丹尼爾 – Hermansson

相關問題