2016-01-27 58 views
0

我嘗試在按鍵輸入框中的某個輸入框中調用事件,但不知道如何傳遞輸入框的值。Ionic,Angular在按鍵事件中獲取輸入類型值

這就是我的做法,最終得到undefined

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> 

什麼是將數據傳遞到角的正確方法?

回答

1

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)"> 
相關問題