【发布时间】:2015-09-24 09:13:01
【问题描述】:
我有发送按钮,然后单击该按钮从输入中发送文本。我还想在输入键盘上发送该文本。我尝试了两种解决方案,但什么都没有..
你可以在下面找到我的代码:
第一个解决方案
<div class="modal-footer">
<input id="enterText" name="enterText" class="form-control" aria-describedby="basic-addon1"
ng-model="modalInstanceController.showText" ng-keypress="keyEnter($event)">
<br/>
<button class="btn btn-primary" type="button" ng-click="modalInstanceController.concatConversation()"
ng-keypress="$event.which === 13 && modalInstanceController.concatConversation()">Send
</button>
</div>
第二种解决方案
我用发送按钮调用这个函数:
$scope.modalInstanceController.concatConversation = function () {
$scope.modalInstanceController.currentConversation.push({
loggedUser: $scope.modalInstanceController.showText,
user: 'typing...'
});
$scope.modalInstanceController.showText = "";
};
我也尝试在keyEnter函数中调用这个函数:
$scope.modalInstanceController.keyEnter = function (keyEvent) {
if (keyEvent.which === 13) {
$scope.modalInstanceController.currentConversation.push({
loggedUser: $scope.modalInstanceController.showText,
user: 'typing...'
});
$scope.modalInstanceController.showText = "";
}
};
$scope.modalInstanceController.keyEnter();
在 html 中:
<button class="btn btn-primary" type="button" ng-click="modalInstanceController.concatConversation()"
ng-keypress="modalInstanceController.keyEnter($event)">Send
</button>
我收到错误:
TypeError: 无法读取未定义的属性 'which'
我应该添加一些依赖还是什么?
【问题讨论】:
标签: javascript html angularjs dom-events event-driven