看起来这里发生了一些简单的事情,但我无法弄清楚出了什么问题(还在学习angularjs)。
我正在使用快速回复选项进行网络聊天,可以从输入或点击快速回复芯片更新模型。芯片的价值应该分配给模型,但问题就出现了。
控制器代码(输入和快速回复功能相同)
// sending message
$scope.sendMessage = function (chip) {
if(chip != ''){
console.log(chip) // getting the chip value correctly
$scope.message.text = chip; // looks like nothing happens here
console.log( $scope.message.text) // undefined
$scope.message.myEntry = true;
$scope.messages.push($scope.message);
socket.emit('chat message', {
myEntry: $scope.message.myEntry,
text: $scope.message.text
}
);
}else{
$scope.message.myEntry = true;
$scope.messages.push($scope.message);
socket.emit('chat message', {
myEntry: $scope.message.myEntry,
text: $scope.message.text
}
);
}
// clear message box
$scope.message = '';
};
视图的代码
<li class="ChatLog__entry item"
ng-class="{ChatLog__entry_mine: message.myEntry === true}"
ng-repeat="message in messages track by $index">
<img class="ChatLog__avatar" src="//placekitten.com/g/50/50" />
<p ng-if="message.text" class="ChatLog__message" ng-class="{clientMessage: message.myEntry === true}">{{message.text}}
<time class="ChatLog__timestamp"></time>
</p>
<md-button ng-if="message.button" class="md-raised md-default" ng-click="gotoState(message.button.state)">{{message.button.value}}</md-button>
<md-chips class="quick_replies" ng-if="message.quick_replies" ng-model="message.quick_replies"
readonly="true">
<md-chip-template ng-model="message.text" readonly="true" md-removable="false"
ng-click="sendMessage($chip)">{{$chip}}</md-chip-template>
</md-chips>
</li>
谢谢。
答案 0 :(得分:1)
$scope.message = '';
这将再次将对象转换为字符串。
将此行更改为
$scope.message = {};