gpt4 book ai didi

javascript - $scope 未捕获 Angular 中的变量

转载 作者:行者123 更新时间:2023-12-03 09:58:50 25 4
gpt4 key购买 nike

我遇到了一个问题,我只是想了解它为什么会这样 - 我正在尝试使用 HTML 页面上的最后一个按钮来获取聊天应用程序的用户“jid”,以及方法我尝试使用 ng-model='jid' 和 ng-model='jid.jid' 来做到这一点,而在 Controller 中尝试以 $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>

最佳答案

不需要需要声明这一点:

$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);
}

关于javascript - $scope 未捕获 Angular 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30648702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com