问题
How do we get the keypress event and its value in Angular on Android? I Use the 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}}
Any help is really appreciated.
回答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}}
UPDATE:
<input type="search" name="userName" ng-model="user" ng-change="getValue(user)" class="search-input" style="width: 96%; margin: 6px auto 6px auto;" placeholder="Начните вводить название">
And my controller $scope.getValue = function (calll) { alert(calll); }
回答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 function available in angularjs,
You can also use ng-keydown and ng-keyup in similar manner. FYI.
For reference , ng-keypress tutorial
回答4:
Creating a directive attribute might work better in this case.
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;
})
In the html add the attribute to the tag.
<input mydirective type="search" name="userName" ng-model="user" class="search-input" style="width: 96%; margin: 6px auto 6px auto;" placeholder="Начните вводить название">
来源:https://stackoverflow.com/questions/28103584/how-to-get-keypress-event-in-angular-on-android