2017-07-16 127 views
0

以下是我的两个文件为简单的投票应用程序,由于某种原因,只有刷新voting.html页面时按钮不会增加计数,两个计数都会增加一个。感谢您的帮助。express-angular-node:投票应用程序

<!-- voting.html--> 
    <!DOCTYPE html> 
    <html> 
    <head> 
     <meta charset="utf-8"> 
     <title>Voting</title> 
     <script src="angular.min.js"></script> 
     <script> 
      var app = angular.module('myFirstApp', []); 

      app.controller('CounterCtrl', function($scope,$http) { 
       console.log('CounterCtrl started'); 

       // $scope.countA = 0; 
       // $scope.countB = 0; 

       // $scope.Acrease = function() { 
       // console.log('Acrease() called'); 
       // $scope.countA++; 
       // }; 
       // $scope.Bcrease = function() { 
       // console.log('Bcrease() called'); 
       // //if($scope.count>0) $scope.count--;$scope.count++; 
       // $scope.countB++; 
       // }; 

       function getItemsA() { 
        $http.get('/voteA').then(function(res) { 
         $scope.countA = res.data; 
         //$scope.countA = res.json(A); 

         console.log('/voteA: ', $scope.countA); 
        }); 
       } 

       getItemsA(); 

       function getItemsB() { 
        $http.get('/voteB').then(function(res) { 
         $scope.countB = res.data; 

         //$scope.countA = res.json(A); 
         console.log('/voteB: ', $scope.countB); 
        }); 
       } 

       getItemsB(); 

       // $scope.addItem = function() { 
       // console.log('addItem()\t newItem=', $scope.newItem); 
       // $http.get('/list/add/' + $scope.newItem).then(getItems); 
       // }; 
      }); 

     </script> 
    </head> 
    <body ng-app="myFirstApp"> 

     <div ng-controller="CounterCtrl"> 
      <p>Count of A Vote: <b>{{countA}}</b></p> 
      <p>Count of B Vote: <b>{{countB}}</b></p> 

      <input type="button" ng-click="getItemsA()" value="A"> 
      <input type="button" ng-click="getItemsB()" value="B"> 
     </div> 

    </body> 
</html> 


/***server.js***/ 
var express = require('express'); 

var app = express(); 

app.listen(3001, function() { 
    console.log('Listening on 3001'); 
}); 

// Mount the public directory at/
app.use('/', express.static('./public')); 


/******************************************** 
* This code is for voting.html 
*******************************************/ 

// initialize the votes variables 
var A = 0 ; 
var B = 0 ; 

// add the voteA 
app.get('/voteA', function(req,res) { 
    //res.json(A); 
    A++ 
    res.json(A); 

}); 

// add the voteB 
app.get('/voteB', function(req,res) { 
    //res.json(A); 
    B++ 
    res.json(B); 
}); 

回答

0

这是我想的。

我认为你的问题在于你的ng-click调用了一个不在范围内的函数。我相信你应该创建范围功能:

$scope.getItemsA = function() { 
    $http.get('/voteA').then(function(res) { 
     $scope.countA = res.data; 
     console.log('response recieved'); 
    }); 
    }; 

希望它可以帮助..