php - angular with mysql to fetch database

708

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);
    }
    }


}) ();
878

Answer

Solution:

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;
}

People are also looking for solutions to the problem: php - Multiple buttons and 'a' tags that post from a single page

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.