我在app.js中关注init函数
$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 an error occurs
// or server returns response with an error status.
});
}
},
我从
开始<body ng-controller="AppController" ng-init="init()">
我想要这样,如果我第一次登录然后我需要登录并且我存储id并传入localstorage并且每次当应用程序加载并且init方法是fire并且我检查id并且从localstorage和fire服务器传递如果自动完成正确的登录,则检查id并传递的方法。
我正在使用phonegap + onsenui + angular js。
问题来自init方法
$scope.ons.navigator.pushPage('dashboard.html',{title : 'title'});
不会重定向到dashborad。
答案 0 :(得分:0)
$scope.ons.navigator.pushPage('dashboard.html',{title : 'title'});
没有错。但我认为在<body>
标记内,没有<ons-navigator>
标记,以便使用该功能。此外,您需要在setTimeout内调用该函数,否则将在DOM元素完成渲染之前调用它。请执行以下操作:
以下是您需要添加到控制器的内容:
app.factory('DataService', function($http) {
var service = {
requestData: function(url) {
return $http.get(url).then(function(data, status, headers, config) {
service.myData = data;
return service.myData;
});
},
myData: null,
return 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'});
}
});
}
}
};
在HTML
中<body ng-controller="AppController" ng-init="init()">
<ons-navigator>
<!--Your HTML-->
</ons-navigator>
</body>