realisation
realisation

Reputation: 638

Angular Scope not working in onclick function

I'm having a problem here where the correct $scope.testMessage is not being passed into QB.chat.send...

Right now what happens when typing new info into the input field tied to $scope.testMessage is an update to the {{testMessage}} in the HTML, which is what we expect, but when punching the button tied to sendMessageClick(), this new value isn't passed into the function -- the old value, here set to "Cheese" is passed into it. Even if it has been changed to "StinkingFrenchCheese" and that is reflected in the {{testMessage}} part on the live HTML. It still only passes "Cheese" to the 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 = "[email protected]"; //countchew
    var starshipenterprise = "[email protected]"; //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>

Upvotes: 0

Views: 237

Answers (1)

orcaman
orcaman

Reputation: 6561

Try passing the message to the on click handler:

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

Then have your onclick handler receive the new message as the input

  $scope.sendMessageClick = function(msg) {

Upvotes: 2

Related Questions