未定义的获取.Angular Factory;Ionic框架错误

Get in undefined. Angular Factory & Ionic Framework Error

本文关键字:Ionic 框架 错误 Factory 获取 Angular 未定义      更新时间:2023-09-26

我正试图在后台使用一个工厂。我遇到了一个注入器错误,所以我把工厂放在了app.js而不是services.js中。这样做解决了注入器问题,但现在当我从app.js调用factory.get时,我得到"不是函数"。

var app = angular.module('starter', ['ionic','ionic.service.core', 'starter.controllers','ngStorage','ngCordova'])
var myService = app.factory('myService', function($localStorage, $scope, $http) {
    var items = $http.get("url + $localStorageVariable").then(function(resp) {
      if (resp) { 
        return resp['data.items'];// This will produce promise, not array so can't call directly
        console.log("Factory success");
        } else {
          console.error('ERR', err);
        }
      });
      return {
        getAll: function() {
            return items;
        }
      }
});

 //later in app.js
         myService.getAll().then(function(items){ 
          console.log("Debug. This line in service call") //this doesnt log.
          console.log(items)
         });

错误

app.js:50未捕获类型错误:myService.getAll不是函数

编辑:

我现在拥有的是:

    var app = angular.module('starter', ['ionic','ionic.service.core', 'starter.controllers','ngStorage','ngCordova'])

app.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    } 
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }

  (function() {
  'use strict';
  angular
    .module('starter', [])
    .controller('MainCtrl', MainCtrl)
    .factory('myService', myService);
  MainCtrl.$inject = ['$scope', 'myService'];
  function MainCtrl($scope, myService) {
    function getSuccess(response) {
      $scope.items = response.data;
    }
    function getError(response) {
      console.log('error');
    }
    myService.getAll()
      .then(getSuccess)
      .catch(getError);
  }
  function myService($http) {
    var factory = {
      getAll: getAll
    };
    return factory;
    function getAll() {
      return $http.get("url");//triple checked, not the isssue
    }
  }
})();

您似乎没有factory注入到controller中。此外,factory的编写方式也有错误。检查以下示例:

(function() {
  'use strict';
  angular
    .module('starter', [])
    .controller('MainCtrl', MainCtrl)
    .factory('myService', myService);
  MainCtrl.$inject = ['$scope', 'myService'];
  function MainCtrl($scope, myService) {
    function getSuccess(response) {
      $scope.items = response.data;
    }
    function getError(response) {
      console.log('error');
    }
    myService.getAll()
      .then(getSuccess)
      .catch(getError);
  }
  function myService($http) {
    var factory = {
      getAll: getAll
    };
    return factory;
    function getAll() {
      return $http.get('url');
    }
  }
})();