在angularjs应用程序

时间:2015-06-27 09:56:40

标签: angularjs pouchdb

我在AngularJS应用程序中使用PouchDB。

以下是我在AngularJS应用程序中实现/访问PouchDB的步骤。

  1. 通过Bower安装angular-pouchdb:

    bower install --save angular-pouchdb
    
  2. 在app.js中添加pouchdb作为模块依赖项:

    var application = angular.module("application", ["ui.router", "pouchdb"]);
    
  3. 在我的应用程序中注入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;
    });
    
  4. 从HomeController调用

    application.controller("HomeController", function ($scope, pouchDataAccessFactory) {
    
        function initialize()
        {
            pouchDataAccessFactory.createDatabase();
        }
        debugger;
        initialize();
    });
    
  5. 添加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>
    
  6. 出现以下错误:

      

    ReferenceError:在Object.factory.createDatabase

    中未定义pouchdb

    我按照以下链接: https://github.com/wspringer/angular-pouchdb

1 个答案:

答案 0 :(得分:0)

从文档中您必须尊重案例:pouchDB而不是pouchdb

angular.factory('factory', function(pouchDB) {
  var db = pouchDB('name');
});

正如评论中所述,检查您是否在html代码中设置了正确的脚本