离子模型登录窗口作为全局方法吗? [英] Ionic Model Login window as global method?
问题描述
我正在使用Ionic开发应用程序.仅某些状态需要登录,因此它是可选的.如果用户未通过身份验证,我想触发一个登录模型.我对整个身份验证进行了排序,但是不确定如何设置全局登录模型.
I am working on an app in Ionic. Only certain states require to be logged in so it's optional. I want to fire a login model if the user is not authenticated. I have the whole authentication sorted, but not sure how I can set a global login model.
例如,默认的启动器应用程序的登录名位于AppCtrl中.我可以通过执行login()从nav模板中执行...
For example the default starter app has the login sitting in the AppCtrl. I can execute from the nav template nav by doing login()... https://github.com/driftyco/ionic-starter-sidemenu/blob/master/js/app.js
如何使它成为全局方法或过程? (例如,启动登录模型,接受帖子并从另一个控制器调用它).
How can I make this a global method or process? (e.g launch a login model, accept post and call it from another controller).
^这需要是工厂还是服务部门?
^ Would this need to be a factory or a service?
我正在做类似的事情.
.controller('SecureCtrl', function ($state, $scope, User) {
if(!isAuthenticated) (){
User.login();
// fires login model as user is not authenticated.
}else{
// do stuff};
}
})
也许有人可以指出正确的方向或一些简单的例子.我看到了有关如何执行此操作的教程,但它们主要用于先登录然后访问所有路由.我仅在某些控制器上需要登录部分.
Maybe someone can point me in the right direction or some simple example. I see tutorials on how to do this, but they are mostly for logging in first and then accessing all routes. I need the login part only on certain controllers.
为了更好地说明这一点,我想创建一个执行登录,注销,显示模型等功能的User函数.因此,我可以简单地从控制器中进行调用. User.login().
To better clarify I am wanting to create a User function which does the login, logout, show model, etc. So I can simply call from my controller. User.login().
(例如)
function User($scope, $ionicModal, $timeout) {
// With the new view caching in Ionic, Controllers are only called
// when they are recreated or on app start, instead of every page change.
// To listen for when this page is active (for example, to refresh data),
// listen for the $ionicView.enter event:
//$scope.$on('$ionicView.enter', function(e) {
//});
// Form data for the login modal
$scope.loginData = {};
// Create the login modal that we will use later
$ionicModal.fromTemplateUrl('templates/login.html', {
scope: $scope
}).then(function(modal) {
$scope.modal = modal;
});
// Triggered in the login modal to close it
$scope.closeLogin = function() {
$scope.modal.hide();
};
// Open the login modal
$scope.login = function() {
$scope.modal.show();
};
// Perform the login action when the user submits the login form
$scope.doLogin = function() {
console.log('Doing login', $scope.loginData);
// Simulate a login delay. Remove this and replace with your login
// code if using a login system
$timeout(function() {
$scope.closeLogin();
}, 1000);
};
}
//然后可以从我的控制器打电话.
// Then from my controller I can just call.
.controller('SecureCtrl', function ($state, $scope, User) {
if(!isAuthenticated) (){
User.login();
// fires login model as user is not authenticated.
}else{
// do stuff};
}
})
如果用户是工厂,服务机构等,我不确定如何执行此操作.将不胜感激.
I am not sure how to do this if User would be a factory, service, etc. Any help would be appreciated .
推荐答案
您可以公开isAuthenticated
状态变量以将登录状态存储在$rootScope
中,并使用它来检查用户的登录状态.
You can expose the isAuthenticated
state variable to store login state in $rootScope
and use it to check the logged in state of the user.
.controller('SecureCtrl', function ($state, $scope, $rootScope, User) {
if(!$rootScope.isAuthenticated) (){
User.login();
// fires login model as user is not authenticated.
} else{
// do stuff
};
})
})
更新:
在您的User.login
中,使用$ionicModal.fromTemplateUrl
方法弹出模态.
UPDATE:
Within your User.login
use the $ionicModal.fromTemplateUrl
method to pop the modal.
$ionicModal.fromTemplateUrl('templates/mylongform.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
});
在此处查看相同的工作实现: http://codepen.io/ionic/pen/VLwLOG
See the working implementation of the same here: http://codepen.io/ionic/pen/VLwLOG
这篇关于离子模型登录窗口作为全局方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!