2016-06-22 55 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

相關問題