我正在创建一个测验,我想将用户给出的答案添加到一个数组 (myAnswers),当测验完成后,我将我的用户重定向到摘要页面,在那里他可以看到正确的答案和他的答案给定的。它们都是不同的控制器。我尝试使用服务进行实验,但这不起作用......
有人可以帮我解决这个问题吗?
服务
var lycheeServices = angular.module('lycheeControllers', [])
lycheeServices.service('myAnswerService', function () {
var myAnswers= [];
this.AddAnswer = function(number, a){
myAnswers[number-1] = a;
};
this.getAnswer = function(number){
return myAnswers[number-1];
};
});
控制器测验
lycheeControllers.controller('quizCtrl', ['$scope', '$http', 'myAnswerService',
function ($scope, $http, myAnswerService) {
$scope.checked = function (answer) {
myAnswerService.addAnswer(number, answer.answer);
}
控制器摘要
lycheeControllers.controller('summaryCtrl', ['$scope', '$http', 'myAnswerService', function ($scope, $http, myAnswerService) {
$scope.myAnswer = myAnswerService.getAnswer(number);
]