Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PockDB:TypeError:angular.factory错误不是函数_Javascript_Angularjs_Angularjs Service_Pouchdb - Fatal编程技术网

Javascript PockDB:TypeError:angular.factory错误不是函数

Javascript PockDB:TypeError:angular.factory错误不是函数,javascript,angularjs,angularjs-service,pouchdb,Javascript,Angularjs,Angularjs Service,Pouchdb,我想知道如何在AngularJS中使用PockDB。我试着按照这些指示去做 我想我在理解创建工厂和/或服务的语法时遇到了问题。我到了“与数据库交互”一节 app.js 'use strict'; angular .module('myappApp', [ 'ngCookies', 'ngResource', 'ngSanitize', 'ngRoute', 'pouchdb' ]) .config(function ($routeProvid

我想知道如何在AngularJS中使用PockDB。我试着按照这些指示去做

我想我在理解创建工厂和/或服务的语法时遇到了问题。我到了“与数据库交互”一节

app.js

'use strict';

angular
  .module('myappApp', [
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngRoute',
    'pouchdb'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  })
angular.factory('someservice', function(pouchdb) {
  // Do something with pouchdb.
  var db = pouchdb.create('testdb');
  pouchdb.destroy('testdb');
  db.put({_id: 'foo', name: 'bar'});
});
当我添加“db.put”时,我在浏览器控制台中看到的消息是:

 [15:17:45.343] TypeError: angular.factory is not a function @ http://127.0.0.1:9000/scripts/app.js:21

angular
对象没有工厂方法,因此它将作为未定义返回

尝试将其放在模块方法返回的对象上

angular
  .module('myappApp', [
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngRoute',
    'pouchdb'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  })
  // note the difference here, it is using the object returned by module() and config()
  .factory('someservice', function(pouchdb) {
    // Do something with pouchdb.
    var db = pouchdb.create('testdb');
    pouchdb.destroy('testdb');
    db.put({_id: 'foo', name: 'bar'});
  });

此外,我还能够调整这个代码示例来测试DB是否正常工作

app.js "严格使用",

angular
  .module('myappApp', [
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngRoute',
    'pouchdb'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  })
  .factory('MyModel', function(pouchdb) {
    return new pouchdb.create('mydb');
  })
  .run(function (){
    var db = new PouchDB('mydb');

    db.info().then(function(info) {
      if (info.doc_count < 2) {
        db.post({display: 'Hello'});
        db.post({display: 'World'});
      }
    });
  });
main.html

  <p>Hello {{options.name}}! There are {{numOfDocs}} docs in your Pouch</p>

  <p>Root scope:</p>
  <ul>
    <li pouch-repeat="item in db" ng-bind="item.display"></li>
  </ul>

  <p>Child scope:</p>
  <ul>
    <li pouch-repeat="item in options.db" ng-bind="item.display"></li>
  </ul>
你好{{{options.name}!你的袋子里有{{numOfDocs}}}个文件

根范围:

子范围:

  <p>Hello {{options.name}}! There are {{numOfDocs}} docs in your Pouch</p>

  <p>Root scope:</p>
  <ul>
    <li pouch-repeat="item in db" ng-bind="item.display"></li>
  </ul>

  <p>Child scope:</p>
  <ul>
    <li pouch-repeat="item in options.db" ng-bind="item.display"></li>
  </ul>