angularjs - $scope.ons.navigator.pushPage not work in init method -
i next init function in app.js
$scope.init = function () { if(localstorage.getitem("id")!=null && localstorage.getitem("pass")!=null) { alert(localstorage.getitem("id")+" "+localstorage.getitem("pass")); var id=localstorage.getitem("id"); var pass=localstorage.getitem("pass"); $http({method: 'get', url: site+'/login-web.php?txt_email='+id+'&txt_password='+pass}). success(function(data, status, headers, config) { if(data=='error') navigator.notification.alert("wrong username or password.",null,"attention.!","try again.!"); else { localstorage.setitem("id", id); localstorage.setitem("pass", password); alert("fire"); $scope.ons.navigator.pushpage('dashboard.html',{title : 'title'}); } }). error(function(data, status, headers, config) { alert("please check mobile data."); // called asynchronously if error occurs // or server returns response error status. }); } },
and fire init
<body ng-controller="appcontroller" ng-init="init()">
i want if login first time need login , store id , pass in localstorage , every time when application load , init method fire , check id , pass localstorage , fire server method check id , pass if right login done automatically.
i using phonegap + onsenui + angular js.
problem init method
$scope.ons.navigator.pushpage('dashboard.html',{title : 'title'});
is not redirect dashborad.
there nil wrong $scope.ons.navigator.pushpage('dashboard.html',{title : 'title'});
. think within <body>
tag, there no <ons-navigator>
tag needed in order utilize function. moreover, need phone call function within settimeout otherwise called before dom element finishes rendering. follows:
here need add together ur controller:
app.factory('dataservice', function($http) { var service = { requestdata: function(url) { homecoming $http.get(url).then(function(data, status, headers, config) { service.mydata = data; homecoming service.mydata; }); }, mydata: null, homecoming service; }); app.controller('appcontroller', function($scope, dataservice)){ $scope.init = function(){ settimeout($scope.init_wait, 10); }; $scope.init_wait = function () { if(localstorage.getitem("id")!=null && localstorage.getitem("pass")!=null){ alert(localstorage.getitem("id")+" "+localstorage.getitem("pass")); var id=localstorage.getitem("id"); var pass=localstorage.getitem("pass"); var url = ite+'/login-web.php?txt_email='+id+'&txt_password='+pass; dataservice.requestdata(url).then(function(data, status, headers, config) { if(data=='error') navigator.notification.alert("wrong username or password.",null,"attention.!","try again.!"); else { localstorage.setitem("id", id); localstorage.setitem("pass", password); alert("fire"); $scope.ons.navigator.pushpage('dashboard.html',{title : 'title'}); } }); } } };
here within html
<body ng-controller="appcontroller" ng-init="init()"> <ons-navigator> <!--your html--> </ons-navigator> </body>
angularjs cordova onsen-ui
No comments:
Post a Comment