2013-07-03 39 views
0

我開始使用AngularJS,並設置一個服務器與nodejs,一切都很好,但是當我打開鉻開發人員工具,我看到該應用程序使兩個Ajax重複調用獲取JSON節點/ mongo發送的數據。 首先我認爲模板玉是問題,但在使用普通的HTML,以同樣的方式在節點快速js重複ajax調用在AngularJS

a demo in appfog

任何想法的作品?

這裏是AngularJS

angular 
.module('foro') 
.config(function($routeProvider){ 
    $routeProvider 
     .when("/",{ 
      templateUrl : "/partials/questions.html", 
      controller: "QuestionsCtrl" 
     }) 
     .when("https://stackoverflow.com/questions/:questionId",{ 
      templateUrl: "/partials/singleQuestion.html", 
      controller: "SingleQuestion" 
     }) 


    }) 
    .controller("QuestionsCtrl", function($scope, $http){ 

     $http.get("/questions").success(function(data){ 
      $scope.questions = data;   

     }); 
    }) 
    .controller("SingleQuestion", function($scope, $http, $routeParams){ 
     var questionId = $routeParams.questionId; 

     $http.get("/question/"+questionId).success(function(data){ 
      $scope.question = data; 
      $scope.votes = data[0].votes; 
     }); 

這是怎麼在節點/ Express服務器

app.get('/questions', function (req, res) { 
    Question.find({}).sort({_id:"descending"}).execFind(function(err,docs){ 
     if(err) res.json(err) 
     res.json(docs) 
    }); 
}); 

app.get('/question/:questionId', function (req, res) { 
    var questionId = req.params.questionId; 

    Question.find({_id: questionId},function(err,docs){ 
     if(err) res.json(err) 
     res.json(docs) 
    }); 
}); 

回答

3

響應代碼的問題是,你有你的部分ng-controller="QuestionsCtrl"

加載部分時,路由配置已將指定控制器分配給ng-view

 $routeProvider 
     .when("/",{ 
      templateUrl : "/partials/questions.html", 
      controller: "QuestionsCtrl" 
     }) 

這導致相同的控制器被嵌套兩次。

從該行<div ng-controller="QuestionsCtrl">questions.html

+0

非常感謝finishingmove問題解決取出NG控制器的一部分! –

+0

沒問題@JoseSosa – finishingmove