我如何使用ngCordova sqlite的服务和科尔多瓦,SQLitePlugin与离子框架? [英] How do I use the ngCordova sqlite service and the Cordova-SQLitePlugin with Ionic Framework?
问题描述
我一直在试图incorperate源码到一个简单的应用程序离子,这是我一直在关注的过程:
离子开始对myApp sidemenu
然后我安装sqlite的插件:
离子插件添加https://github.com/brodysoft/Cordova-SQLitePlugin
和ngCordova
亭子安装ngCordova
这给了我以下选项:
无法找到角度合适的版本,请选择一个:
1)角#1.2.0它解析为1.2.0,并通过ngCordova#0.1.4-α需要
2)角#> = 1.0.8它解析为1.2.0,由角UI路由器#0.2.10要求
3)角#1.2.25这决定1.2.25,由角动画#1.2.25,角的sanitize#1.2.25要求
4)角#〜1.2.17这决定1.2.25,是由离子#1.0.0-beta.13 $ P $所需PFIX的选择!把它坚持到bower.json
我挑选项3)
我包括在该文件中的脚本如下:
<脚本SRC =LIB /离子/ JS / ionic.bundle.js>< / SCRIPT>
<脚本SRC =LIB / ngCordova /距离/ NG-cordova.js>< / SCRIPT>
&所述; SCRIPT SRC =cordova.js>&下; /脚本>
<脚本SRC =JS / app.js>< / SCRIPT>
<脚本SRC =JS / controllers.js>< / SCRIPT>
然后我增加了一个控制器到搜索视图:
.controller('SearchCtrl',函数($范围,$ cordovaSQLite){
的console.log('测试');
变种DB = $ cordovaSQLite.openDB({名:my.db}); //打开背景DB:
变种DB = $ cordovaSQLite.openDB({名:my.db,bgType:1}); $ scope.execute =功能(){
的console.log('测试');
VAR的查询=INSERT INTO TEST_TABLE(数据,data_num)VALUES(?,?);
$ cordovaSQLite.execute(DB,查询,[测试,100]),然后(功能(RES){
的console.log(insertId:+ res.insertId);
},功能(错误){
console.error(ERR);
});
};
})
这导致错误:
>类型错误:无法读取的未定义的属性'的openDatabase
>在Object.openDB(HTTP://本地主机:8100 / lib目录/ ngCordova /距离/ NG-cordova.js:2467:36)
接下来,我试过手动包括SQLitePlugin.js方式:
从复制的的插件/ com.brodysoft.sqlitePlugin / WWW 的以主 WWW / 的并将其添加到索引中。 html页面
我试过,包括之前的一切:
<脚本SRC =SQLitePlugin.js>< / SCRIPT>
<脚本SRC =LIB /离子/ JS / ionic.bundle.js>< / SCRIPT>
<脚本SRC =LIB / ngCordova /距离/ NG-cordova.js>< / SCRIPT>
&所述; SCRIPT SRC =cordova.js>&下; /脚本>
<脚本SRC =JS / app.js>< / SCRIPT>
<脚本SRC =JS / controllers.js>< / SCRIPT>
我得到错误的的ReferenceError:未定义科尔多瓦
所以我又试图将它收入cordova.js脚本之后,但仍然得到同样的错误
倒很AP preciate帮助
如果它是相关的,这些都是科尔多瓦的当前版本和离子我使用的:
离子 - 版本1.2.5
科尔多瓦--version 3.5.0-0.2.7
和这是生成bower.json
{
名:对myApp
私:真正的,
devDependencies:{
离子:driftyco /离子鲍尔#1.0.0-beta.13
}
}
和我的package.json:
{
名:的myapp
版本:1.0.0
说明:对myApp:离子项目
依赖:{
吞掉:^ 3.5.6
吞掉-萨斯:^ 0.7.1
吞掉-CONCAT:^ 2.2.0
吞掉-缩小CSS的:^ 0.3.0
吞掉-重命名:^ 1.2.0
},
devDependencies:{
亭子:^ 1.3.3
吞掉-UTIL:^ 2.2.14
shelljs:^ 0.3.0
}
}
如果有人试图在浏览器中运行它时仍然有错误,试试这个:
如果(window.cordova){
DB = $ cordovaSQLite.openDB({名:my.db}); //设备
}其他{
DB = window.openDatabase(my.db,1,我的',1024 * 1024 * 100); //浏览器
}
I have been trying to incorperate sqlite into a simple Ionic app and this is the process I have been following:
ionic start myApp sidemenu
Then I install the sqlite plugin:
ionic plugin add https://github.com/brodysoft/Cordova-SQLitePlugin
and ngCordova
bower install ngCordova
this gave me the following options: Unable to find a suitable version for angular, please choose one: 1) angular#1.2.0 which resolved to 1.2.0 and is required by ngCordova#0.1.4-alpha 2) angular#>= 1.0.8 which resolved to 1.2.0 and is required by angular-ui-router#0.2.10 3) angular#1.2.25 which resolved to 1.2.25 and is required by angular-animate#1.2.25, angular-sanitize#1.2.25 4) angular#~1.2.17 which resolved to 1.2.25 and is required by ionic#1.0.0-beta.13Prefix the choice with ! to persist it to bower.json
I picked option 3) and I included the scripts in the file as follows:
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
I then added a controller to the search view:
.controller('SearchCtrl', function ($scope, $cordovaSQLite){
console.log('Test');
var db = $cordovaSQLite.openDB({ name: "my.db" });
// for opening a background db:
var db = $cordovaSQLite.openDB({ name: "my.db", bgType: 1 });
$scope.execute = function() {
console.log('Test');
var query = "INSERT INTO test_table (data, data_num) VALUES (?,?)";
$cordovaSQLite.execute(db, query, ["test", 100]).then(function(res) {
console.log("insertId: " + res.insertId);
}, function (err) {
console.error(err);
});
};
})
This caused the error:
> TypeError: Cannot read property 'openDatabase' of undefined
> at Object.openDB (http://localhost:8100/lib/ngCordova/dist/ng-cordova.js:2467:36)
Next I tried manually including the SQLitePlugin.js by: copying from plugins/com.brodysoft.sqlitePlugin/www to main www/ and adding it to the index.html page
I tried including before everything:
<script src="SQLitePlugin.js"></script>
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
I get Error ReferenceError: cordova is not defined so I then tried including it after the cordova.js script but still get the same error
would really appreciate the help in case it is relevant, these are the current versions of Cordova and ionic I am using:
ionic --version 1.2.5
cordova --version 3.5.0-0.2.7
and this is the generated bower.json
{
"name": "myApp",
"private": "true",
"devDependencies": {
"ionic": "driftyco/ionic-bower#1.0.0-beta.13"
}
}
and my package.json:
{
"name": "myapp",
"version": "1.0.0",
"description": "myApp: An Ionic project",
"dependencies": {
"gulp": "^3.5.6",
"gulp-sass": "^0.7.1",
"gulp-concat": "^2.2.0",
"gulp-minify-css": "^0.3.0",
"gulp-rename": "^1.2.0"
},
"devDependencies": {
"bower": "^1.3.3",
"gulp-util": "^2.2.14",
"shelljs": "^0.3.0"
}
}
If someone still got an error when trying to run it in a browser, try this one:
if (window.cordova) {
db = $cordovaSQLite.openDB({ name: "my.db" }); //device
}else{
db = window.openDatabase("my.db", '1', 'my', 1024 * 1024 * 100); // browser
}
这篇关于我如何使用ngCordova sqlite的服务和科尔多瓦,SQLitePlugin与离子框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!