AngularJS按钮单击不触发功能

时间:2016-07-24 02:59:14

标签: javascript angularjs node.js

我正在尝试修改此问题:https://github.com/amirrajan/nodejs-against-humanity

使用我编写的游戏规则。其中一个最重要的部分是,应该有一个带有按钮的文本框,而不是白卡。当我按提交时,没有任何反应 - Javascript警报不会触发或任何事情。

game.html:

<div class="row" ng-show="showWhiteCardList()">
  <table id="whiteCards" class="table">
    <tbody id="whiteCardSelection">
      <tr>
        <td>
          <textarea ng-model="situationAppendage"></textarea>
          <button class="btn btn-default" ng-click="selectSituation(situationAppendage)">Submit</button>
        </td>
      </tr>
    </tbody>
  </table>
</div>

services.js:

angular.module('myApp.services', [])
  .factory('GameService', function($http) {
        var s4 = function() {
            return Math.floor(Math.random() * 0x10000).toString();
        };
        var guid = function(){
            return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
        };
        var pId = guid();

        return {
            playerName: '',
            playerId : pId,
            newGameId : guid(),
            currentGameId: undefined,
            initName: function() {
                if(this.playerName.length === 0) {
                    this.playerName = 'anonymous ' + s4();
                }
            },
            getGames: function() {
                return $http.get('/list');
            },
            createGame: function() {
                return $http.post('/add', { id: guid(), name: this.playerName + "'s game" });
            },
            joinGame: function(gameId, playerId, name) {
                return $http.post("/joingame", { gameId: gameId, playerId: playerId, playerName: name });
            },
            departGame: function(gameId, playerId) {
                $http.post('/departgame', { gameId: gameId, playerId: playerId});
            },
            selectCard: function(gameId, playerId, selectedCard){
                $http.post("/selectCard", { gameId: gameId, playerId: playerId, whiteCardId: selectedCard });
            },
            selectSituation: function(gameId, playerId, textBoxText){
                $http.post("/selectSituation", { gameId: gameId, playerId: playerId, textBoxText : textBoxText });
            },
            selectWinner: function(gameId, selectedCard) {
                $http.post("/selectWinner", { gameId: gameId, cardId: selectedCard });
            },
            readyForNextRound: function(gameId, playerId) {
                $http.post("readyForNextRound",  { playerId: playerId, gameId: gameId });
            }
        };
    });

controllers.js:

$scope.selectSituaton = function(textToAppend) {
  alert('Situation fired');
  GameService.selectSituaton($scope.gameId, $scope.playerId, textToAppend);
};

server.js:

app.post('/selectSituation', function(req, res) {
  console.log("Triggered");
  Game.selectSituation(req.body.gameId, req.body.playerId, req.body.textBoxText);
  broadcastGame(req.body.gameId);
  returnGame(req.body.gameId, res);
});

我是Node.JS,AngularJS和Javascript的新手,所以我可能会遗漏一些明显的东西。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:2)

问题是控制器中的拼写错误与selectSituation 。目前它是 $ scope.selectSituaton ,而它应该是 $ scope.selectSituation

尝试使用以下代码:

$scope.selectSituation = function(textToAppend) {
  alert('Situation fired');
  GameService.selectSituaton($scope.gameId, $scope.playerId, textToAppend);
};

我相信它会解决您的问题。

干杯!