【问题标题】:How to get keypress event in angular on Android?如何在Android上以角度获取按键事件?
【发布时间】:2015-01-23 05:28:09
【问题描述】:

我们如何在 Android 上的 Angular 中获取按键事件及其值? 我使用 phonegap Cordova Angular JS

 <form name="myForm">
 <input type="search" name="userName" ng-model="user"  class="search-input" style="width: 96%; margin: 6px auto 6px auto;" placeholder="Начните вводить название">
 </form>
 </div>
 </div>
 user = {{user}}

非常感谢任何帮助。

【问题讨论】:

  • ng-keypress="yourmethod(user)"

标签: javascript android angularjs cordova keypress


【解决方案1】:
 <form name="myForm">
     <input type="search" 
            name="userName" 
            ng-model="user" 
            ng-keypress="yourMethod(user)" class="search-input" 
            style="width: 96%; margin: 6px auto 6px auto;" 
            placeholder="Начните вводить название">
 </form>

 user = {{user}}

更新:

<input type="search" name="userName" ng-model="user" ng-change="getValue(user)" class="search-input" style="width: 96%; margin: 6px auto 6px auto;" placeholder="Начните вводить название">

还有我的控制器$scope.getValue = function (calll) { alert(calll); }

【讨论】:

  • ng-change="yourMethod(user)".
  • 这是我的输入 我的控制器 $scope.getValue = function (call) { alert(callll); } 但我看到警报未定义
  • ng-model 将您的视图绑定到控制器.. ng-model="user" 表示您在输入框中键入的值是用户..
  • 我在 android 上遇到了同样的问题,试过 ng-change 但对我不起作用。 stackoverflow.com/q/58113528/3081929
【解决方案2】:
<form name="myForm">
 <input type="search" name="userName" ng-keypress="getValue($event) ng-model="user"  class="search-input" style="width: 96%; margin: 6px auto 6px auto;" placeholder="Начните вводить название">
 </form>
 </div>
 </div>
 user = {{user}}


In controller :-

$scope.getValue = function (event) { console.log(event) }

【讨论】:

    【解决方案3】:

    ng-keypress 功能在 angularjs 中可用,

    您也可以以类似的方式使用 ng-keydownng-keyup。仅供参考。

    供参考, ng-keypress tutorial

    【讨论】:

      【解决方案4】:

      在这种情况下,创建指令属性可能会更好。

      angular.module('inputDirective', [])
          .directive("mydirective", function() {
              var directive = {};
              directive.restrict = 'A';
              directive.scope = {};
              directive.link = function(scope, element, attrs, controller) {
                  //read the text typed in the div
                  function read() {
                      var html = element.html();
                  }
      
                  //do this whenever someone starts typing
                  element.bind("keyup", function() {
                      scope.$apply(read);
                  });
              }
      
              return directive;
          })
      

      在 html 中将属性添加到标签中。

      <input mydirective type="search" name="userName" ng-model="user"  class="search-input" style="width: 96%; margin: 6px auto 6px auto;" placeholder="Начните вводить название">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 2015-03-22
        • 2022-10-25
        • 2011-03-05
        • 1970-01-01
        相关资源
        最近更新 更多