我有服务
angular.module('myapp')
.service('AnswerService', function() {
var answers = [];
this.addAnswers = function(questionId) {
answers.push(questionId);
console.log(answers);
}
return this;
})
并且必须在控制器中检索答案的值并以html格式显示。
我应该使用return this
还是return answers
?
答案 0 :(得分:2)
添加另一个getter函数,例如
angular.module('myapp')
.service('AnswerService', function() {
var answers = [];
this.addAnswers = function(questionId) {
answers.push(questionId);
console.log(answers);
}
this.getAnswers = function() {
return answers;
}
});
答案 1 :(得分:0)
如果要访问外部数组,则应将其设置为服务。
添加行
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;
})
答案 2 :(得分:0)
this.answers = answers;
我会返回this
答案 3 :(得分:0)
您应该将代码更改为此。
angular.module('myapp').service('AnswerService', function() {
var answers = [];
this.addAnswers = function(questionId) {
answers.push(questionId);
console.log(answers);
return answers;
} })