将值从控制器传递到工厂以在对象之间进行选择

时间:2017-04-20 12:37:29

标签: angularjs factory

我有一个在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;
            }  

}

1 个答案:

答案 0 :(得分: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;
        }  
    } 
}

从控制器中,您可以获取数据:

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