0

I am starting using AngularJS, and setup a server with nodejs, everything goes great, but when i open chrome developers tools i see that the app make two ajax repeat calls for get the json data that node/mongo send. first i think that the template jade was the problem, but after use plain html, works in the same way

a demo in appfog

any idea?

here is the code in AngularJS

angular
.module('foro')
.config(function($routeProvider){
    $routeProvider
        .when("/",{
            templateUrl : "/partials/questions.html",
            controller: "QuestionsCtrl"
        })
        .when("/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;
      });

this is how respond in node/express server

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)
  });
});
Graham
  • 7,431
  • 18
  • 59
  • 84
Jose Sosa
  • 2,157
  • 3
  • 17
  • 18

1 Answers1

3

The problem is that you have ng-controller="QuestionsCtrl" in your partial.

The route config already assigns the designated controller to ng-view when you load the partial.

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

This results in the same controller being nested twice.

Remove the ng-controller part from this line <div ng-controller="QuestionsCtrl"> in questions.html

holographic-principle
  • 19,688
  • 10
  • 46
  • 62