AngularJS无法从数据库获取最新数据

时间:2016-08-08 00:23:51

标签: angularjs

我有一个在页面加载时调用的factory.all()函数。但是,每当我在MongoDB中输入新条目或删除一条时,它都不会令人耳目一新。我正在将数据记录到控制台以查看返回的对象数。目前,我有四个被退回。即使我删除其中一个并刷新页面,仍会有4个对象记录到控制台。这是浏览器缓存问题吗?有办法对付这个吗?

我的控制器:

angular.module('quizCtrl', [])

.controller('quizController', function(Quiz) {
    // Assign local variable to be used in application
    var vm = this;      
    // To Load All Quizzes on Application
    Quiz.all().success(function(data) {
        console.log(data);
    }); 

我的工厂: angular.module('quizService',[])

.factory('Quiz', function($http) {

// create a new object
var quizFactory = {};

// get a single quiz
quizFactory.get = function(id) {
    return $http.get('/api/quiz/' + id);
};

// get all quizzes
quizFactory.all = function() {
    return $http.get('/api/quiz/');
};

// create a quiz
quizFactory.create = function(quizData) {
    return $http.post('/api/quiz/', quizData);
};

// update a quiz
quizFactory.update = function(id, quizData) {
    return $http.put('/api/quiz/' + id, quizData);
};

// delete a quiz
quizFactory.delete = function(id) {
    return $http.delete('/api/quiz/' + id);
};

// return our entire quizFactory object
return quizFactory;

});

端点: // GET请求立即加载所有测验         apiRouter.route('/ quiz')

        .get(function(req, res) {

            Quiz.find({}, function(err, quiz) {
                if (err) res.json({ message: "Error: " + err})
                res.json(quiz);
            });
        });

    // GET request to get a specific quiz only
    apiRouter.route('/quiz/:quiz_id')

        // get the user with that id
        .get(function(req, res) {
            Quiz.findById(req.params.quiz_id, function(err, quiz) {
                if (err) res.json("Error: " + err);

                // return that user
                res.json(quiz);
            });
        });

HTML显示数据

<div class="panel panel-success" ng-show="quiz.quiz">
    <div class="panel-heading">
        <div class="panel-title">{{ quiz.quiz.question }} 
            <div style="float:right">
                {{ ex.createdAt | date : "MMM d, y h:mm a" }}
            </div>
        </div>
    </div>
        <div class="panel-body">
            <button type="button" class="btn btn-default" ng-click="quiz.submitAnswer(quiz.quiz.answers[0], quiz.quiz._id, quiz.quiz.correctAnswer)" ng-disabled="quiz.isDisabled">A. {{ quiz.quiz.answers[0] }}</button><br><br>
            <button type="button" class="btn btn-default" ng-click="quiz.submitAnswer(quiz.quiz.answers[1], quiz.quiz._id, quiz.quiz.correctAnswer)"" ng-disabled="quiz.isDisabled">B. {{ quiz.quiz.answers[1] }}</button><br><br>
            <button type="button" class="btn btn-default" ng-click="quiz.submitAnswer(quiz.quiz.answers[2], quiz.quiz._id, quiz.quiz.correctAnswer)" ng-disabled="quiz.isDisabled">C. {{ quiz.quiz.answers[2] }}</button><br><br>
            <button type="button" class="btn btn-default" ng-click="quiz.submitAnswer(quiz.quiz.answers[3], quiz.quiz._id, quiz.quiz.correctAnswer)"" ng-disabled="quiz.isDisabled">D. {{ quiz.quiz.answers[3] }}</button><br><br>
            <div class="alert alert-info" role="alert" ng-if="(quiz.successMessage)">
                <strong>{{ quiz.successMessage }}</strong> is correct.
            </div>
            <div class="alert alert-danger" role="alert" ng-if="(quiz.failureMessage)">
                <strong>{{ quiz.failureMessage }}</strong> is incorrect.
            </div>
            <div class="alert alert-success" role="alert" ng-if="(quiz.correctMessage)">
                Correct answer is: <strong>{{ quiz.correctMessage }}</strong>
            </div>
                Correct So Far: {{ quiz.correctSoFar }}
            </div>


            <div class="panel-body">
                <div class="panel-body">Posted by: {{ exam.postedBy }}</div>
                <div style="float:right">
                <button class="btn btn-success" type="button" ng-click="quiz.nextQuestion()" ng-if="quiz.isDisabled">Next</button>
                </div>  
            </div>      
        </div>

</div>

0 个答案:

没有答案