How to get the input text value with AngularJS?

1

I'm trying to get the value of an input and save it in a variable so I can use the result as a value in LIKE , of a query of sqlite . But I came across a small problem: when I try to get the value of the input the next result is undefined , but if I use a controller in the div that involves the input, then the correct result comes. I'm trying to get this value from the function inside the template controller.

Then I had the following question: Is it possible for me to take the value of the input with some function that is inside the controller of this template or will I have to create another controller just to get the input value? And if I have to create another controller, how can I get and use it in the query SQL ?

Edited

I'll put here my controller code and HTML, it might be easier to understand:

myApp.controller("ServicesCtrl", function($scope, $ionicPlatform, $cordovaSQLite) {
  
  var result = '';

  $ionicPlatform.ready(function() {
    $scope.myClick = function() {
      result = $scope.searchkey;
      console.log(result);
    };

    var query = "SELECT serviceName FROM tblServices WHERE serviceName LIKE '%"+ result +"%' ";
    $cordovaSQLite.execute(db, query, []).then(function(res) {
      if(res.rows.length > 0) {
        for(var i = 0; i < res.rows.length; i++) {
          console.log(res.rows.item(i).serviceName);
        }
      }
    }, function(err) {
      console.log(err);
    });
  });

});
<div class="bar bar-header item-input-inset">
  <label class="item-input-wrapper">
    <i class="icon ion-ios-search placeholder-icon"></i>
    <input type="text" placeholder="Buscar..." name="key" autocorrect="off" ng-model="searchkey">
  </label>
  <button class="button" ng-click="myClick()">Buscar</button>
</div>
    
asked by anonymous 17.08.2016 / 03:38

1 answer

1

I do not understand why your controller is making the select out of the click function, I imagine you want to do the select once you load the ionic, but the function does not have to be within the ready.

In the controller I always set the default value that I will use in the scope variables, just as I did with the result

myApp.controller("ServicesCtrl", function($scope, $ionicPlatform,     $cordovaSQLite) {

  var result = '';
  $scope.searchkey = '';

  $scope.myClick = function() {
      result = $scope.searchkey;
      console.log(result);
  };

  $ionicPlatform.ready(function() {

    //desta forma chama o select com o result = '' assim que carregar o ionic
    var query = "SELECT serviceName FROM tblServices WHERE serviceName LIKE '%"+ result +"%' ";
    $cordovaSQLite.execute(db, query, []).then(function(res) {
      if(res.rows.length > 0) {
        for(var i = 0; i < res.rows.length; i++) {
          console.log(res.rows.item(i).serviceName);
        }
      }
    }, function(err) {
      console.log(err);
    });
  });

});

If you do not solve the problem, there is a way to pass the value of the input as a parameter in the function:

<div class="bar bar-header item-input-inset">
    <label class="item-input-wrapper">
       <i class="icon ion-ios-search placeholder-icon"></i>
       <input type="text" placeholder="Buscar..." name="key" autocorrect="off" ng-model="searchkey">
    </label>
    <button class="button" ng-click="myClick(searchkey)">Buscar</button>
</div>

In this case you just have to set your myClick function to receive the parameter:

$scope.myClick = function(search) {
    result = search;
    console.log(result);
};
    
17.08.2016 / 14:49