dependency injection in angular /factories #services

This commit is contained in:
2017-01-31 23:27:31 -08:00
parent 1c95615de6
commit 08cfc94820
4 changed files with 51 additions and 4 deletions

View File

@@ -4,14 +4,16 @@
.module("turtleFacts")
.controller("listCtrl", ListController);
function ListController () {
ListController.$inject = ['quizMetrics'];
function ListController (quizMetrics) {
// List controller logic
var vm = this;
vm.quizMetrics = quizMetrics;
vm.data = turtlesData;
vm.activeTurtle = {}; // will be used in the view to hold the data of currently active turtle
vm.search = ""; // Adding the Search property to be used in the ng-model
vm.quizActive = false;
vm.changeActiveTurtle = changeActiveTurtle;
vm.activateQuiz = activateQuiz;
@@ -20,7 +22,7 @@
}
function activateQuiz () {
vm.quizActive = true;
quizMetrics.changeState(true);
}
}

17
js/controllers/quiz.js Normal file
View File

@@ -0,0 +1,17 @@
(function () {
angular
.module("turtleFacts")
.controller("quizCtrl", QuizController);
QuizController.$inject = ['quizMetrics'];
function QuizController (quizMetrics) {
var vm = this;
vm.quizMetrics = quizMetrics;
}
}) ();

View File

@@ -0,0 +1,21 @@
(function () {
angular
.module("turtleFacts")
.factory("quizMetrics", QuizMetrics);
function QuizMetrics() {
var quizObj = {
quizActive: false,
changeState: changeState
};
return quizObj;
function changeState(state) {
quizObj.quizActive = state;
}
}
}) ();