0

I receive an error while I'm saving my promise in my angular controller:

The controller is:

angular.module('app.controllers')
  .controller('questionController', function($log,QuizCreate,QuestionBank){

    QuestionBank.get().then(function(response){
      this.questions = response.data;
      $log.info(response.data);
    });

    // this.quiz = QuizCreate.generateQuiz();

    $log.info(this.questions);

  }); 

And the error I receive is:

TypeError: Cannot set property 'questions' of undefined

Why???

PSL
  • 122,084
  • 19
  • 250
  • 241
Roberto Pezzali
  • 2,320
  • 1
  • 25
  • 44

1 Answers1

2

This failed because the this refers to the anonymous function not to the controller.

angular.module('app.controllers')

.controller('questionController', function($log,QuizCreate,QuestionBank){
       var that=this;
QuestionBank.get().then(function(response){
  that.questions = response.data;
  $log.info(response.data);
});

// this.quiz = QuizCreate.generateQuiz();

$log.info(this.questions);

  }); 
cirtrus
  • 497
  • 5
  • 16