1.添加sqlite 组件

cordova plugin add https://github.com/litehelpers/Cordova-sqlite-storage.git --save

2.编写代码 app.js  =>run=> $ionicPlatform.ready 添加

if(window.cordova ) {
ltdb = $cordovaSQLite.openDB({name: "ltapp.db", location: 1});
//创建用户表
$cordovaSQLite.execute(ltdb, "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT, phonenumber INTEGER, password TEXT)"); $cordovaSQLite.execute(ltdb, 'SELECT COUNT(*) FROM users ').then(function (res) {
if (res.rows.length < 0) {
var query = "INSERT INTO users (name,email,phonenumber,password) VALUES ( ?, ?, ?, ?)";
$cordovaSQLite.execute(ltdb, query, ['admin', 'sulin11026@163.com', '13683365354', '123']).then(function (result) {
console.log("success");
}, function (error) {
})
}
},
function (error) {
console.log(error);
}
);
}

controllers .js

controller('LoginCtrl',['$scope', '$state', '$ionicPopup', 'AuthService', function($scope, $state, $ionicPopup, AuthService) {
$scope.login = function() {
var user= $scope.user;
AuthService.login(user.uname, user.pwd).then(function(authenticated) { $state.go('tabs.home', {}, {reload: true});
$scope.setCurrentUsername(user.username);
}, function(err) {
var alertPopup = $ionicPopup.alert({
title: '登录失败',
template: '请输入正确的账号密码!'
});
});
};
}])

servcies.js

.factory('AuthService',['$q', '$http','$cordovaSQLite', function($q, $http,$cordovaSQLite) {var username = '';var login = function(name, pw) {
return $q(function(resolve, reject) {
try {
$cordovaSQLite.execute(ltdb, "SELECT * FROM users WHERE name = ? ",[name]).then(
function(res) {
if (res.rows.length > 0) {
if(pw.toLocaleUpperCase()==res.rows.item(0).password.toLocaleUpperCase()){
resolve('Login success.');
}
}else{
reject('Login Failed.');
}
},
function(error) {
reject('Login Failed.');
}
);
}
catch(ex){
}
});
};return {
login: login
};
}])

index.html

<ion-view class="login" hide-nav-bar="true" style="z-index: 1" cache-view="false">
<form ng-submit="login()" name="theform">
<div id="login">
<div class="logo"><img src="img/fixed/logo.png"></div>
<div class="fill">
<label class="item item-input ion-ios-person-outline">
<input type="text" placeholder="账号" ng-model="user.uname"
ng-focus="hide='1'" ng-blur="hide='1'" ng-required="true" >
</label>
<label class="item item-input ion-ios-locked-outline">
<input type="password" placeholder="密码" ng-model="user.pwd" ng-required="true"
ng-focus="hide='1'" ng-blur="hide='1'" >
</label>
<label class="btn">
<button class="button button-block button-light" type="submit">登录</button>
</label>
</div>

以上测试正常通过,如果有问题需要耐心慢慢调试

Ionic Cordova Sqlite 实现保存用户名登陆的更多相关文章

  1. 搭建 AngularJS+Ionic+Cordova 开发环境并运行一个demo

    目前的手机APP有三类:原生APP,WebAPP,HybridApp:HybridApp结合了前两类APP各自的优点,越来越流行. Cordova就是一个中间件,让我们把WebAPP打包成Hybrid ...

  2. ionic 使用sqlite

    昨天被ionic和sqlite折腾一天,怎么也无法实现读取,后来才发现,原来是codova中的sqliteplugin版本问题. 问题:Database location or iosDatabase ...

  3. 基于ionic+cordova+angularJs从零开始搭建自己的移动端H5 APP

    这里详细介绍下如何用ionic+cordova+angularjs搭建自己的移动端app,包括环境搭建,框架使用等,具体项目已放置在github上,可下载下来自行启动. 下载地址:https://gi ...

  4. ionic+cordova+angularJs

    ionic+cordova+angularJs 这里详细介绍下如何用ionic+cordova+angularjs搭建自己的移动端app,包括环境搭建,框架使用等,具体项目已放置在github上,可下 ...

  5. cookie保存用户名及密码

    登陆页中,用户输入用户名密码,点击提交,后台对照mysq数据库中,看是否有对应的用户名,以及密码是否正确.如果正确 则将用户名密码分两份Cookie保存.页面跳转到登陆成功页. 用户再次访问登陆页时, ...

  6. 用户登录保存数据实例(慕课笔记 使用SharedPreferences保存用户名)

    学习视频之后自己操作时的笔记. 0.视频地址:http://www.imooc.com/video/3265 1.功能预览: 说明:1)输入错误用户名和密码,点击登录,弹出提示框“禁止登录”: 2)输 ...

  7. NSUserDefaults保存用户名和密码

    #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...

  8. Ionic Cordova 调用原生 Api 实现拍照上传 图片到服务器功能

    Ionic 调用 Device 设备 Api 获取手机的设备信息 1. 找到对应的Api: https://ionicframework.com/docs/native/device/ 2. 安装相关 ...

  9. Ionic + Cordova 跨平台移动开发环境配置

    1.下载安装JDK(根据各自系统选择32位或64位下载),安装完成之后需要做以下环境变量配置 在“系统变量”中,设置3象属性,JAVA_HOME,PATH,CLASSPATH(大小写无所谓),如果已经 ...

随机推荐

  1. type元类创建类的方法

    一.代码 class_name='car' dict_name={} bases=(object,) class_body=''' def __init__(self,name): self.name ...

  2. Heartbeat基本介绍----HA / vmware HA FT

    Heartbeat是High-Availability Linux Project (Linux下的高可用性项目)的产物,是一套提供防止业务主机因不可避免的意外性或计划性宕机问题的高可用性软件.Hea ...

  3. 软件-开发软件:Android Studio

    ylbtech-软件-开发软件:Android Studio Android Studio 是谷歌推出的一个Android集成开发工具,基于IntelliJ IDEA. 类似 Eclipse ADT, ...

  4. Linux unzip解压多个文件

    假设当前目录下有多个zip文件 data.zip invoices.zip pictures.zip visit.zip, 直接 unzip *.zip 等价于 unzip data.zip invo ...

  5. IENumerable_Test

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  6. <jquery>滚动例子

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. svg path命令

    参考:https://www.jianshu.com/p/c819ae16d29b         https://www.cnblogs.com/guxuelong/p/7743736.html M ...

  8. SUMMARY | 二分查找

    package Search; public class biSearch { //标准的二分查找 public static int stdBiSearch(int[] array,int keyV ...

  9. Ubuntu环境下安装Scala以及安装IntelliJ Scala插件(Plugin)

    一.Scala介绍 1.结合Spark处理大数据 这是Scala的一个主要应用,而且Spark也是那Scala写的. 2.Java的脚本语言版  可以直接写Scala的脚本,也可以在.sh直接使用Sc ...

  10. JavaScript特效源码(5、背景特效)

    1.数字时钟 背景时钟[好大的钟][推荐][共1步] ====1.以下是这个效果的全部代码.[最好从一个空页面开始] <html> <head> <TITLE>背景 ...