0

I have this working example where i have use $scope and $http in controller to fetch an col from database using get method in variable as given below

 <script>
var fetch = angular.module('myapp', []);

fetch.controller('userCtrl', ['$scope', '$http', function ($scope, $http) {
 $http({
  method: 'get',
  url: 'quizdb.php'
 }).then(function successCallback(response) {
  // Store response data
  $scope.users = response.data;
 });
}]);

</script>

now i want this same thing in my factory service i have where i am using hardcoded array. i want to replace the hardcoded array with dynamic array. with php i am getting an array of but the problem is that i dont know how to implement this thing in factory in angular my factory is as follows

(function(){

angular
   .module("htcssquiz")
   .factory("DataService",DataService);

   function DataService(){
    var dataObj = {
        turtleData: turtleData,
        quizQuestions: quizQuestions,
        correctAnswer : correctAnswer
    };
    return  dataObj;
   }

var correctAnswer = [1,2,3,0,2,0,3,2,0,3];

   var quizQuestions  = [
        {
            type: "text",
            text: "How much can a loggerhead weigh?",
            possibilities: [
                {
                    answer: "Up to 20kg"
                },
                {
                    answer: "Up to 115kg"
                },
                {
                    answer: "Up to 220kg"
                },
                {
                    answer: "Up to 500kg"
                }
            ],
            selected: null,
            correct: null
        }

so i want to replace this correctAnswer array with dynamic one.

please help me i am new to angular . thank you in advance.

I am using this factory DataService in The List controller using $inject as follows

(function(){

angular
    .module("htcssquiz")
    .controller("listctrl", ListController);



 ListController.$inject = ['quizMetric','DataService'];

    function ListController(quizMetric,DataService){

        var vm = this;

            vm.quizMetric =quizMetric;

            vm.data = DataService.turtleData; 

          vm.activeTurtle = {};
         vm.changeActiveTurtle = changeActiveTurtle;
         vm.activateQuiz =activateQuiz;
         vm.search = "";

function changeActiveTurtle(index){

    vm.activeTurtle = index;
}

    function activateQuiz(){
       quizMetric.changeState("quiz", true);
    }
    }


}) ();
5
  • Note: this looks like angular 1, which has its own tag angularjs, you should update it. Commented Mar 2, 2018 at 10:56
  • ok i will from next project because this is almost my complete prject if i able to get data dynamic so please help me with this Commented Mar 2, 2018 at 10:57
  • i'll try when i have a little more time, however i don't really see the connexion between the first code and the second, as the first seems to get users and the second to provide quizz questions, and the 2 modules seems to be unrelated (no dependency). Can you update so that we can clearly see where you want what? Commented Mar 2, 2018 at 11:03
  • Why not something like this: return $http.get('quizdb.php').then((res)=>{var dataObj = res.data; return dataObj;})? Or something indirect: ... dataObj.quizQuestions = res.data.questions; ..., depending on data returned Commented Mar 2, 2018 at 11:13
  • what i want is to make all variable having hardcode arrray replace with dynamic array Commented Mar 3, 2018 at 5:07

1 Answer 1

2

This will require a change to both your controller AND your service. The controller will now use the service as if it were the $http call:

fetch.controller('userCtrl', ['$scope', 'DataService', function ($scope, DataService) {
  DataService.getCorrectAnswer().then(function (response) {
    // Store response data
    $scope.correctAnswer = response.data;
  });
}]);

Your service will now take responsibility for making the $http call:

DataService.$inject = ['$http'];
function DataService($http){
  var dataObj = {
    ...
    getCorrectAnswer : function() {
      return $http({
        method: 'get',
        url: 'quizdb.php'
      });
    }

  };
  return  dataObj;
}
Sign up to request clarification or add additional context in comments.

2 Comments

DataService is Factory not controller
and i am not using $scope also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.