[英]AngularJS button click not triggering function
I am trying to modify this: https://github.com/amirrajan/nodejs-against-humanity 我正在尝试对此进行修改: https : //github.com/amirrajan/nodejs-against-humanity
To work with the rules of a game I made up. 为了配合游戏规则,我制定了。 One of the most significant parts is that, instead of white cards, there should be a single text box with a button to submit.
最重要的部分之一是,应该有一个带有提交按钮的单个文本框,而不是白卡。 When I press submit, nothing happens - The Javascript alert doesn't fire or anything.
当我按Submit时,什么也没发生-Javascript警报不会触发或发生任何事情。
game.html: 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: 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: controllers.js:
$scope.selectSituaton = function(textToAppend) {
alert('Situation fired');
GameService.selectSituaton($scope.gameId, $scope.playerId, textToAppend);
};
server.js: 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);
});
I'm new to Node.JS, AngularJS, and Javascript, so I'm probably missing something obvious. 我是Node.JS,AngularJS和Javascript的新手,所以我可能缺少明显的东西。 Any help is appreciated!
任何帮助表示赞赏!
Issues is typo error with selectSituation in controller. 问题是控制器中的selectSituation出现拼写错误 。 For now it is $scope.selectSituaton while it should be $scope.selectSituation .
现在是$ scope.selectSituaton,而应该是$ scope.selectSituation 。
Try with below code: 尝试以下代码:
$scope.selectSituation = function(textToAppend) {
alert('Situation fired');
GameService.selectSituaton($scope.gameId, $scope.playerId, textToAppend);
};
I believe it will resolve your issue. 我相信它将解决您的问题。
Cheers! 干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.