Angular Scope不在onclick函数中工作

时间:2015-06-12 17:02:26

标签: javascript angularjs

我在这里遇到问题,正确的$ 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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

尝试将消息传递给on click处理程序:

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

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

  $scope.sendMessageClick = function(msg) {