ng-keypress无法在angularjs

时间:2015-09-24 09:13:02

标签: javascript html angularjs javascript-events event-driven

我有发送按钮,然后点击该按钮我从输入发送文本。我也想在输入键盘上发送该文本。我尝试了两种解决方案而且没有...

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

第一个解决方案

<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

我应该在依赖性上添加一些东西吗???

3 个答案:

答案 0 :(得分:1)

你可以尝试

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>

答案 1 :(得分:1)

我正在添加一些主要的事件概念

  1. 如果浏览器支持event.which,则使用event.which,否则使用event.keyCode
  2. event.keycode / event.which将读取哪个键strocked 以及它将返回的相对数值。
  3. 你可以在javascript中测试下面的代码,只需复制下面的代码并尝试使用它。

      

    &lt; input type =“text”onkeypress =“init(event)”&gt;

         

    function init(event){

         

    var x = event.which || event.keyCode;

         

    警报(X);

         

    }

    这不是关于 ng-keypress ,只是哪个没有响应值,它将取决于浏览器,因为某些值会来,所以使用上面提到的代码

答案 2 :(得分:0)

只需将表格标记包围起来

<div class="modal-footer">
<form>
                <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>
</form>
        </div>