2016-03-06 60 views
0

我用來設置我的角度項目的生成器的所有內容都以簡寫方式編碼並且分佈很奇怪,所以我正在努力通過試圖讓我的舊項目進入他們的形式,所以當我構建它時項目會正確部署我的腳本。角度工廠服務問題,未知供應商

所以我當前的問題是,我在我的舊項目中的控制器相互通信,以將ID從我的主控制器傳遞到我的圖庫控制器。我通過父母控制器完成了這一切。現在,當我跳躍到這種新格式時,它似乎不會以相同的方式工作,而應該使用服務。

所有這一切都說我通過我目前的設置,我的服務有問題。無論什麼原因,無論我做什麼我的服務都沒有在我的項目中定義。我意識到我的項目注入器將我的服務文件放在我的控制器文件後面,因爲我認爲這將是問題,但我嘗試將代碼放在我的路由文件中,但仍然存在問題。

如果有人可以查看我的代碼,並檢查什麼是錯的,甚至可以給我一個關於如何從我的主傳遞ID到我的畫廊的一點見解,我將非常感激!

由於

Service.js

(function() { 

'使用嚴格';

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

功能mainService(){ VAR主;

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 
+0

發佈您的所有代碼是沒有必要的。什麼是必要的完整和準確的錯誤信息。 –

+0

對不起,我添加了錯誤信息到最後。 – Jleibham

+0

嗯,我使用gulp注入我的腳本到我的html中,但我查看了網絡面板和所有這些似乎很好。 – Jleibham

回答

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

這是不正確。您沒有指定服務的名稱。它應該是

angular.module('bhamDesign').factory('mainService', mainService); 
+0

明白了,我曾經有過這樣的情況,但仍然在畫錯誤。我只是將它改回來,它似乎正在繪製一個不同但相似的ReferenceError:mainServices未定義錯誤。我會更新我的代碼和錯誤以反映它。 – Jleibham

+0

沒有必要。你正在返回'mainServices'而不是'mainService'。 –

+0

啊!非常感謝。當我花費那麼多時間排除故障時,我總是會踢我自己,它最終會變成一個愚蠢的錯字... – Jleibham