ReferenceError:未定义ArticleService

时间:2015-10-03 09:50:27

标签: angularjs node.js cordova ionic pouchdb

我无法解决这几天,查了很多其他帖子,请帮帮忙。 使用此页面中的示例: http://gonehybrid.com/how-to-use-pouchdb-sqlite-for-local-storage-in-your-ionic-app/

我正在使用

  • 离子
  • 科尔多瓦
  • PouchDB
  • AngularJS

我的环境

  • Cordova CLI:5.3.1
  • 离子版:1.1.0
  • Ionic CLI版本:1.6.5
  • Ionic App Lib版本:0.3.9
  • 操作系统:Mac OS X Mavericks
  • 节点版本:v0.12.7

controllers.js

 angular.module('starter.controllers', [])

 .controller('OverviewCtrl', function($scope, $ionicPlatform) {
  var vm = this;

  // Initialize the database.
  $ionicPlatform.ready(function() {
    ArticleService.initDB();

    // Get all articles records from the database.
    ArticleService.getAllArticles().then(function(articles) {
      vm.articles = articles;
    });
  });
  
/*
  // Initialize the modal view.
  $ionicModal.fromTemplateUrl('templates/playlists.html', {
    scope: $scope
  }).then(function(modal) {
    $scope.modal = modal;
  });
*/
  
  vm.showAddArticleModal = function() {
    $scope.article = {};
  };

  vm.showEditArticleModal = function(article) {
    $scope.article = article;
  };

  $scope.saveArticle = function() {
      ArticleService.addArticle($scope.article);				
  };

  return vm;
})

Playlists.html

<ion-view view-title="List">
  <ion-content>        
    <ion-list>
      <ion-item ng-controller="OverviewCtrl as vm" ng-repeat="a in vm.articles" ng-click="vm.showEditArticleModal(a)">
        <div style="display: block;margin-left: auto;margin-right: auto">{{ a.image }}</div>
        <div style="float: center">{{ a.descr }}</div>
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>

services.js

angular.module('starter.services', [])

.factory('ArticleService', ['$q', function($q) {
 
  var _db;    
  var _articles;

  return {
    initDB: initDB,

    getAllArticles: getAllArticles,
    addArticle: addArticle,
    
    ...
  };

  function initDB() {
    // Creates the database or opens if it already exists
    _db = new PouchDB('articles', {adapter: 'websql'});
  };

  function addArticle(article) {
    return $q.when(_db.post(article));
  };

  ...

}])

menu.html

...

<ion-item menu-close href="#/app/playlists">
  List
</ion-item>

...

apps.js

.state('app.playlists', {
  url: '/playlists',
  views: {
    'menuContent': {
      templateUrl: 'templates/playlists.html',
      controller: 'OverviewCtrl'
    }
  }
})

camera.html

 <ion-modal-view>
   ...
   
   <div class="padding">
      <button ng-controller="OverviewCtrl" ng-click="saveArticle()" class="button button-block button-positive activated">Spara</button>
    </div>
   
   ... 
</ion-modal-view>

我创建了一个ArticleService来保存来自camera.html的数据。问题已经在构建,ArticleService无法在控制器中引用。

2 个答案:

答案 0 :(得分:1)

目前您在ArticleService中使用OverviewCtrl,但您没有将其注入控制器功能。修复它改变

.controller('OverviewCtrl', function($scope, $ionicPlatform) {

.controller('OverviewCtrl', function($scope, $ionicPlatform, ArticleService) {

答案 1 :(得分:0)

我已经解决了这个问题。我的代码中有几个问题。主要在Services.js。将ArticleService的函数标题更改为以。

开头

.factory(&#39; ArticleService&#39;,[&#39; $ q&#39;,ArticleService]); function ArticleService($ q){

还更改了Controller.js中的OverviewCtrl的函数头

.controller(&#39; OverviewCtrl&#39;,[&#39; $ scope&#39;,&#39; $ ionicModal&#39;,&#39; $ ionicPlatform&#39;,&#39; ArticleService&#39;,function($ scope,$ ionicModal,$ ionicPlatform,ArticleService){