Angular Factory服务问题,未知提供商

时间:2016-03-06 16:48:19

标签: angularjs service

我用来设置我的角度项目的生成器的所有内容都编写得很简单并且分布很奇怪所以我正在尝试将旧项目放到他们的表单中,所以当我构建它时,项目将正确地部署我的脚本。

所以我当前的问题是我的旧项目中的控制器彼此通信以将ID从我的主控制器传递到我的图库控制器。我通过父控制器完成了这一切。现在,当我实现这种新格式的飞跃时,似乎它不会以相同的方式工作,我应该使用服务。

说了这么多,我通过目前的设置遇到了我的服务问题。无论我做什么原因,我的服务都没有在我的项目中定义。我意识到我的项目注入器将我的服务文件放在我的控制器文件后面,因为我认为这将是问题,但我尝试将代码放在我的路由文件中,但仍然存在问题。

如果有人可以查看我的代码并查看什么是错误的,甚至可以给我一些关于如何将ID从我的主页传递到我的画廊的一些信息,我将非常感激!

由于

Service.js

(function() {

'use strict';

角     .module( 'bhamDesign')     .factory('mainService',mainService)

function mainService(){     var main;

var mainService = {
  setMain: function(input) {
    main = input;
  },
  getMain: function() {
    return main;
  }
    };
    return mainServices;
  }

})();

Module.js

(function() {
  'use strict';

  angular
    .module('bhamDesign', ['ngTouch', 'ngSanitize', 'ngAria', 'ngResource', 'ui.router', 'mm.foundation']);

})();

Route.js

    (function() {
  'use strict';

  angular
    .module('bhamDesign')
    .config(routerConfig);

  /** @ngInject */
  function routerConfig($stateProvider, $urlRouterProvider) {
    $stateProvider
      .state('home', {
        url: '/',
        templateUrl: 'app/main/main.html',
        controller: 'MainController',
        controllerAs: 'main'
      })
      .state('gallery', {
        url: '/mainId',
        templateUrl: 'app/gallery/gallery.html',
        controller: 'GalleryController',
        controllerAs: 'gallery'
      });

    $urlRouterProvider.otherwise('/');
  }

})();

MainController.js

    (function() {
  'use strict';

  angular
    .module('bhamDesign')
    .controller('MainController', MainController);

  function MainController($scope, mainService, $http, $state) {

  $http.get('../../assets/json/projects.json').success(function(data){
    $scope.mains = data;
  });

  $scope.orderProp = '-year';

  $scope.setId = function(){
    mainServices.setMain($scope.id);
    $state.go('gallery');
  };
}

})();

GalleryController.js

    (function() {
  'use strict';

  angular
    .module('bhamDesign')
    .controller('GalleryController', GalleryController);

  function GalleryController($scope, $state, $http) {
    $http.get('.../.../assets/json/galleries/' + mainServices.getMain() + '.json').success(function(data) {
        $scope.gallery = data;
    });
  }

})();

main.html中

    .row
  .small-12.medium-3.columns.left(ng-repeat="main in mains | orderBy:orderProp | filter:categoryFilter:true")
    .thumbnail-container   
      a(ng-click="setId")
        img.thumbnail(ng-src="{{main.thumbnail}}")
        .text 
          h5 {{main.title}}
          h6 {{main.year}}

错误消息

ReferenceError: mainServices is not defined
    at Object.mainService (http://localhost:3000/app/index.service.js:19:12)
    at Object.invoke (http://localhost:3000/bower_components/angular/angular.js:4604:19)
    at Object.enforcedReturnValue [as $get] (http://localhost:3000/bower_components/angular/angular.js:4443:37)
    at Object.invoke (http://localhost:3000/bower_components/angular/angular.js:4604:19)
    at http://localhost:3000/bower_components/angular/angular.js:4403:37
    at getService (http://localhost:3000/bower_components/angular/angular.js:4550:39)
    at injectionArgs (http://localhost:3000/bower_components/angular/angular.js:4574:58)
    at Object.instantiate (http://localhost:3000/bower_components/angular/angular.js:4616:18)
    at http://localhost:3000/bower_components/angular/angular.js:9870:28
    at http://localhost:3000/bower_components/angular-ui-router/release/angular-ui-router.js:4081:28 <div ui-view="" class="ng-scope">(anonymous function) @ angular.js:13236(anonymous function) @ angular.js:9965invokeLinkFn @ angular.js:9494nodeLinkFn @ angular.js:8978compositeLinkFn @ angular.js:8226publicLinkFn @ angular.js:8106(anonymous function) @ angular.js:8447updateView @ angular-ui-router.js:4021(anonymous function) @ angular-ui-router.js:3959Scope.$broadcast @ angular.js:17143$state.transition.resolved.then.$state.transition @ angular-ui-router.js:3352processQueue @ angular.js:15552(anonymous function) @ angular.js:15568Scope.$eval @ angular.js:16820Scope.$digest @ angular.js:16636Scope.$apply @ angular.js:16928done @ angular.js:11266completeRequest @ angular.js:11464requestLoaded @ angular.js:11405

1 个答案:

答案 0 :(得分:4)

angular.module('bhamDesign').factory(mainService);

这是不正确的。您没有指定服务的名称。它应该是

angular.module('bhamDesign').factory('mainService', mainService);
相关问题