我在AngularJS应用程序中使用PouchDB。
以下是我在AngularJS应用程序中实现/访问PouchDB的步骤。
通过Bower安装angular-pouchdb:
bower install --save angular-pouchdb
在app.js中添加pouchdb作为模块依赖项:
var application = angular.module("application", ["ui.router", "pouchdb"]);
在我的应用程序中注入pouchdb服务:
application.factory("pouchDataAccessFactory", function (pouchdb) {
var factory = {};
var database = undefined;
factory.createDatabase = function ()
{
database = pouchdb.create("memorydb")
.then(function () { })
.catch(function () { })
.finally(function () { });
}
factory.destroyDatabase = function () {
pouchdb.destroy("memorydb");
}
factory.insert = function (product) {
database.put(product)
.then(function (response) {
// Do something with the response
})
.catch(function (error) {
// Do something with the error
})
.finally(function () {
// Do something when everything is done
});
}
return factory;
});
从HomeController调用
application.controller("HomeController", function ($scope, pouchDataAccessFactory) {
function initialize()
{
pouchDataAccessFactory.createDatabase();
}
debugger;
initialize();
});
添加angular-pouchdb.js并在index.html中添加引用
<html>
<head>
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/angular-ui-router.min.js"></script>
<!--<script src="Scripts/pouchdb-3.6.0.js"></script>-->
<script src="Scripts/angular-pouchdb.js"></script>
<script src="App/app.js"></script>
<script src="App/Controllers/HomeController.js"></script>
<script src="App/Factory/PouchDBAccessFactory.js"></script>
<link href="StyleSheets/AppStyle.css" rel="stylesheet" />
</head>
<body>
<div ng-app="application" ng-strict-di>
<nav role="navigation">
<ul>
<li><a ui-sref="home">Home</a></li>
<li><a ui-sref="about">About</a></li>
</ul>
</nav>
<div ui-view>
</div>
</div>
</body>
</html>
出现以下错误:
ReferenceError:在Object.factory.createDatabase
中未定义pouchdb
答案 0 :(得分:0)
从文档中您必须尊重案例:pouchDB
而不是pouchdb
angular.factory('factory', function(pouchDB) {
var db = pouchDB('name');
});
正如评论中所述,检查您是否在html代码中设置了正确的脚本