我尝试在其中一个输入框中按键期间调用事件,但不确定如何传递输入框的值。
这就是我的做法,并最终得到未定义
。
controller.js
$scope.bySearch = function(descr){
var xhr = $http({
method: 'post',
url: 'http://mywebsite.com/api/lists.php?descr='+descr
});
xhr.success(function(data){
$scope.data = data.data;
});
$ionicScrollDelegate.scrollTop();
console.log(descr);
}
sample.html
<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="text" ng-model="dash.search" placeholder="Search" ng-keypress="bySearch(dash.search)">
</label>
将数据传递给 Angular 的正确方法是什么?
最佳答案
sample.html
<div ng-controller="SearchCtrl">
<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="text" ng-model="dash.search" placeholder="Search" ng-change="bySearch(dash.search)">
</label>
</div>
controller.js
var routerApp = angular.module('routerApp', ['ui.router', 'ngRoute']);
routerApp.controller('SearchCtrl', function($scope, $http) {
$scope.bySearch = function(descr){
alert("Inside bySearch--"+descr);
var xhr = $http({
method: 'post',
url: 'http://mywebsite.com/api/lists.php?descr='+descr
});
xhr.success(function(data){
$scope.data = data.data;
});
$ionicScrollDelegate.scrollTop();
console.log(descr);
}
});
这将在每次按键时触发 bySearch() 函数。
ng-keypress 在按键被按下时发生,并且在值填充输入之前发生。这就是为什么您在第一次按键时没有获得任何值,但在后续按键时您将获得该值。
您还可以使用 ng-keyup
<input type="text" ng-model="dash.search" placeholder="Search" ng-keyup="bySearch(dash.search)">
关于javascript - Ionic、Angular 在按键事件上获取输入类型值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35027427/