Reputation: 115
I have service
angular.module('myapp')
.service('AnswerService', function() {
var answers = [];
this.addAnswers = function(questionId) {
answers.push(questionId);
console.log(answers);
}
return this;
})
and have to retrieve the value of answers in controller and display it in html.
should I use return this
or return answers
?
Upvotes: 1
Views: 443
Reputation: 387
You should change your code to this.
angular.module('myapp').service('AnswerService', function() {
var answers = [];
this.addAnswers = function(questionId) {
answers.push(questionId);
console.log(answers);
return answers;
} })
Upvotes: 0
Reputation: 2079
Add another getter function, like,
angular.module('myapp')
.service('AnswerService', function() {
var answers = [];
this.addAnswers = function(questionId) {
answers.push(questionId);
console.log(answers);
}
this.getAnswers = function() {
return answers;
}
});
Upvotes: 2
Reputation: 17943
If you want to access array outside, it should be set to the service.
Add line
this.answers = answers;
angular.module('myapp')
.service('AnswerService', function() {
var answers = [];
this.addAnswers = function(questionId) {
answers.push(questionId);
console.log(answers);
}
this.answers = answers;
return this;
})
Upvotes: 0