gpt4 book ai didi

javascript - ng-keypress 调用 angularjs 中的函数

转载 作者:行者123 更新时间:2023-12-02 15:38:17 26 4
gpt4 key购买 nike

我有发送按钮,单击该按钮我会从输入发送文本。我还想在输入键盘上发送该文本。我尝试了两种解决方案,但什么也没有..

您可以在下面找到我的代码:

第一个解决方案

<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: Cannot read property 'which' of undefined

我应该添加一些依赖项还是什么?

最佳答案

你可以试试

if (keyEvent.keyCode === 13) 

还有一件事

<button class="btn btn-primary" type="button" ng-click="modalInstanceController.concatConversation()"
ng-keypress="$event.keyCode === 13 && modalInstanceController.concatConversation?modalInstanceController.concatConversation():''">Send
</button>

关于javascript - ng-keypress 调用 angularjs 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32757573/

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