0

I'm having a problem I just want to understand why it is behaving as it is - I'm trying to use the last button on the HTML page to get a user 'jid' for a chat application, and the way I tried to do it is with ng-model='jid' and ng-model='jid.jid', while in the controller trying to define it in a few ways like $scope.jid' or $scope.jid = ""; or $scope.jid = {};

I'm confused why they aren't working, because when I put $scope.user.jid, using the $scope.user = {} I assume, it works fine. But not when I made a new $scope variable with $scope.jid? When I do that, the console prints out an empty object.

.controller('ChatCtrl', function($scope, $stateParams, $rootScope, $ionicLoading) {
  console.log("Inside ChatCtrl");
  
  QB.createSession(function(err,result){
    console.log('Session create callback', err, result);
    console.log(JSON.stringify(err));
    console.log(JSON.stringify(result));
  });


  $scope.settings = {};
  $scope.user = {};
  $scope.error = {};
  $scope.jid = {};

  $scope.signInClick = function() {
    console.log('Login was clicked');
     
    var params = {'login': ($scope.user.username), 'password': ($scope.user.password)}
      console.log("params... " + JSON.stringify(params));

      QB.users.create(params, function(err, user){
        if (user) {
          console.log("successful user.create... " + JSON.stringify(user));
          var jid = user.id + "-23837" + "@chat.quickblox.com";
          console.log(user.login + "'s jid is......" + jid);
          var chatparams = {'jid': jid, 'password': ($scope.user.password)};
          QB.chat.connect(chatparams, function(err, roster) {
            console.log(JSON.stringify(err));
            console.log(JSON.stringify(roster));
          });
        } 
        else  {
          console.log(JSON.stringify(err));
          if (err.message == "Unprocessable Entity"){
            QB.login(params, function(err, user){
              if (user) {
                console.log("Logged into QB with " + JSON.stringify(user));
                var jid = user.id + "-23837" + "@chat.quickblox.com";
                console.log(user.login + "'s jid is......" + jid);
                var chatparams = {'jid': jid, 'password': ($scope.user.password)};
                QB.chat.connect(chatparams, function(err, roster) {
                  console.log("stringifying the err... " + JSON.stringify(err));
                  console.log("stringifying the roster... " + JSON.stringify(roster));
                });
              } 
              else  {
                console.log(JSON.stringify(err));
              }
            });
          }
        }
      });

  $scope.getJidClick = function(){
    var jid = $scope.jid.jid
    console.log("jid is.... " + JSON.stringify(jid));
  }
  $scope.sendMessageClick = function() {
    console.log('sendMessageclick');
    QB.chat.send(jid, {
      type: 'chat',
      body: 'Hello world!'
    });
  };
  })
          <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="user.message">
          </label>
          <button class="button button-full button-balanced" data-ng-click="sendMessageClick()">
           Send your message
          </button>
          <label class="item item-input">
            <input type="text" placeholder="jid" ng-model="jid">
          </label>
          <button class="button button-full button-balanced" data-ng-click="getJidClick()">
            Which user?
          </button>

1 Answer 1

1

You don't need to declare this:

$scope.jid = {}; //The controller will pick its value automatically

And inside getJidClick

$scope.getJidClick = function(){
    var jid = $scope.jid; //This should do the job
    console.log("jid is.... " + jid);
  }
Sign up to request clarification or add additional context in comments.

3 Comments

Its still saying undefined with this
@Femtosecond Can you create a fiddle?
hmmm its one part of a big app, how would I do that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.