$ scope无法在Angular

时间:2015-06-04 15:59:42

标签: javascript angularjs

我遇到了一个问题我只想了解它为什么会这样做 - 我正在尝试使用HTML页面上的最后一个按钮来为聊天应用程序获取用户'jid',以及方式我尝试使用ng-model ='jid'和ng-model ='jid.jid',而在控制器中尝试以几种方式定义它,如$ scope.jid'或$ scope.jid =“ “;或$ scope.jid = {};

我很困惑他们为什么不工作,因为当我把$ scope.user.jid放在我认为使用$ scope.user = {}时,它运行正常。但是当我用$ scope.jid创建一个新的$ scope变量时呢?当我这样做时,控制台会打印出一个空对象。

.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 个答案:

答案 0 :(得分:1)

需要声明:

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

getJidClick

$scope.getJidClick = function(){
    var jid = $scope.jid; //This should do the job
    console.log("jid is.... " + jid);
  }