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
  • 123,204
  • 21
  • 253
  • 243
Roberto Pezzali
  • 2,484
  • 2
  • 27
  • 56

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