How to set input field value on load controller?

Hello Sir,
I have used bellow controller. In the controller i have used a function init(). I want to call the function onload controller. and set data into input filed .

.controller(‘SignInCtrl’, function($scope, $state, $timeout,$ionicPopup,$filter,$cordovaSQLite) {

    $scope.init = function() {
    var query = "SELECT user_id FROM useridinfo";
    $cordovaSQLite.execute(db, query).then(function(res) {
        if(res.rows.length > 0) {    
            $rootScope.loaduserid = res.rows.item(0).user_id;
        } else {
            console.log("No results found");
        }
    }, function (err) {
        console.error(err);
    });
}

})

My input field
input type=“text” placeholder=“User ID” ng-model="user.uname"
I want to set $rootScope.loaduserid data into ng-model=“user.uname” onload view
That means i want to set $rootScope.loaduserid data above the input filed

Please help me…

Before .init function:
$scope.user = {};

inside the if:
$scope.user.uname = $rootScope.loaduserid

Angular will do the rest.

Check if it works :smile:

Hello siderreira
I have used
.state(‘signin’, {
url: “/signin”,
templateUrl: “signin.html”,
controller: ‘SignInCtrl’
})
$urlRouterProvider.otherwise("/signin");

The problem is that SignInCtrl is running first. Then $ionicPlatform.ready(function() {}) call under the run function. Sqlite data not found because SignInCtrl run before device ready.

How to call init function after device ready in SignInCtrl
.
Please Help me…

Is your code in a git repo?
Seem to have something wrong…

Here is my github Link

Ok, first of all, I suggest you to refactor your code. Maybe you will need to learn a few tricks to do it, but, place your SQLite queries inside a service will make the code cleaner.

Place the SQLite call inside an $scope.init function. Use this inside the ng-init of one element on your view. So you will make the cal when the view is actually loaded.

The proper way is to use Promises inside your SQLite-related service, but it’s hard tome to explain here/now.

Hello Sir,
I have also used ng-init But it also not working.

Thanks for your co-operation. I have done It .

lol, I just finished https://github.com/SidFerreira/showUserID :stuck_out_tongue:

Hello Sir,
Thanks a lot for your co-operation