2016-06-22 57 views
1
app.controller('AdminUserCtrl', function ($scope, $controller, $location, $http, $rootScope, $pouchDB, $state, $stateParams) { 
    $controller('AdminCtrl', {$scope: $scope}); 
    console.log("AdminUser Controller reporting for duty."); 

    $scope.items = {}; 

    $pouchDB.startListening(); 

    // try to call 
    $pouchDB.getUsers(); 

    console.log($pouchDB.getUsers()); 


    // Listen for changes which include create or update events 
    $rootScope.$on("$pouchDB:change", function (event, data) { 
     $scope.items[data.doc._id] = data.doc; 
     $scope.$apply(); 
    }); 

    // Listen for changes which include only delete events 
    $rootScope.$on("$pouchDB:delete", function (event, data) { 
     delete $scope.items[data.doc._id]; 
     $scope.$apply(); 
    }); 

    // Look up a document if we landed in the info screen for editing a document 
    if ($stateParams.documentId) { 
     $pouchDB.get($stateParams.documentId).then(function (result) { 
      $scope.inputForm = result; 
     }); 
    } 

app.service("$pouchDB", ["$rootScope", "$q", function ($rootScope, $q) { 

     var database; 
     var changeListener; 
     this.setDatabase = function (databaseName) { 
      database = new PouchDB(databaseName); 
     }; 
     this.getUsers = function() { 
      return database.query({ 
       map: function (doc, emit) { 
        if (doc.type === "user") { 
         emit(doc._id, doc); 
        } 
       } 
      }); 
     }; 
     this.startListening = function() { 
      changeListener = database.changes({ 
       live: true, 
       include_docs: true 
      }).on("change", function (change) { 
       if (!change.deleted) { 
        $rootScope.$broadcast("$pouchDB:change", change); 
       } else { 
        $rootScope.$broadcast("$pouchDB:delete", change); 
       } 
      }); 
     }; 

我试图创建视图/查询分贝......但没有任何反应......我试图创建视图/查询分贝......但没有任何反应...

任何人都可以提供示例如何在angularjs-pouchdb中创建视图?

console.log($ pouchDB.getUsers());

回报:

无极{[PromiseStatus]: “待定”,[PromiseValue]:未定义}

回答

1

按照文档,你想要的东西,看起来像。 。 。

app.service("$pouchDB", ["$rootScope", "$q", function ($rootScope, $q) { 
    var instance = {}; 
    instance.database = null; 
    instance.changeListener = null; 
    instance.setDatabase = function (databaseName) { 
     this.database = new PouchDB(databaseName); 
    }; 
    instance.getUsers = function() { 
     return this.database.query({ 
      map: function (doc, emit) { 
       if (doc.type === "user") { 
        emit(doc._id, doc); 
       } 
      } 
     }); 
    }; 
    instance.startListening = function() { 
     this.changeListener = this.database.changes({ 
      live: true, 
      include_docs: true 
     }).on("change", function (change) { 
      if (!change.deleted) { 
       $rootScope.$broadcast("$pouchDB:change", change); 
      } else { 
       $rootScope.$broadcast("$pouchDB:delete", change); 
      } 
     }); 
    }; 
    return instance; 
}]); 
+0

你能提供一个链接到文档中的地方吗?我找不到这样的东西:( –

+1

https://docs.angularjs.org/guide/services阅读“注册服务”部分: – 2ps

相关问题