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>