繁体   English   中英

Angular Scope在onclick函数中不起作用

[英]Angular Scope not working in onclick function

我在这里遇到问题,其中正确的$ scope.testMessage没有传递到QB.chat.send中。

现在,在与$ scope.testMessage关联的输入字段中输入新信息时会发生什么,这是HTML中{{testMessage}}的更新,这是我们期望的,但是当按下与sendMessageClick()关联的按钮时,新值不会传递到函数中,而是将旧值(此处设置为“ Cheese”)传递给该函数。 即使已将其更改为“ StinkingFrenchCheese”,这也反映在实时HTML的{{testMessage}}部分中。 它仍然仅将“奶酪”传递给sendMessageClick。

  $scope.testMessage = "Cheese"; console.log($scope.testMessage); $scope.sendMessageClick = function() { console.log("message = " + $scope.testMessage); var user = $rootScope.user; console.log('sendMessageclick'); var countchew = "3354163-23837@chat.quickblox.com"; //countchew var starshipenterprise = "3354099-23837@chat.quickblox.com"; //starshipenterprise if (user == "countchew"){ QB.chat.roster.confirm(starshipenterprise, function(){ console.log("roster.confirm called"); }); QB.chat.roster.add(starshipenterprise, function() { console.log("roster.add called"); }); var chewparams = {type: 'chat', name: 'testMessage', body: ($scope.testMessage), extension: {save_to_history: 1}}; QB.chat.send(starshipenterprise, chewparams); } else if (user == "starshipenterprise"){ QB.chat.roster.confirm(countchew, function() { console.log("roster.confirm called"); }); QB.chat.roster.add(countchew, function() { console.log("roster.add called"); }); var starparams = {type: 'chat', name: 'testMessage', body: ($scope.testMessage), extension: {save_to_history: 1}}; QB.chat.send(countchew, starparams); } }; 
 <ion-view view-title="Chat"> <ion-content> <center class="padding-vertical">Chat 'em up</center> <!-- Begin Login Form --> <div class="list list-inset"> <label class="item item-input"> <input type="text" placeholder="User Name" ng-model="user.username"> </label> <label class="item item-input"> <input type="text" placeholder="Password" ng-model="user.password"> </label> <button class="button button-full button-balanced" data-ng-click="signInClick()"> Sign In </button> <label class="item item-input"> <input type="text" placeholder="Input your message!" ng-model="inputMessage"> </label> <button class="button button-full button-balanced" data-ng-click="sendMessageClick()"> Send your message </button> <button class="button button-full button-balanced" data-ng-click="getCurrentParseUser()"> Get Current Parse User </button> <button class="button button-full button-balanced" data-ng-click="QBDisconnect()"> Disconnect from QB chat </button> <p>The message is : {{testMessage}}</p> <p>Debug area says : {{debug}}</p> <label class="item item-input"> <input type="text" placeholder="Give some text!" ng-model="testMessage"> </label> <button class="button button-full button-balanced" data-ng-click="sendMessageClick()"> Send your message </button> </div> <!-- End Login Form --> </ion-content> </ion-view> 

尝试将消息传递给点击处理程序:

  <button class="button button-full button-balanced" data-ng-click="sendMessageClick(testMessage)">

然后让您的onclick处理程序接收新消息作为输入

  $scope.sendMessageClick = function(msg) {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM