2017-02-26 1 views
0

angular.js:14362 Error: [$injector:unpr] Unknown provider: reportTypeServiceProvider <- reportTypeService <- ReportTypeController 오류가 발생합니다.

나는 이것에 관해 많은 자료를 찾았지만 여전히 문제가 내 코드에 무엇인지 파악할 수 없습니다. 여기있다 :

pitchviz.js :

(function() { 
    'use strict'; 

    angular 
    .module('pitchviz', [ 
     'pitchviz.config', 
     'pitchviz.routes', 
     'pitchviz.report-types' 
    ]); 

    angular 
    .module('pitchviz.config', []); 

    angular 
    .module('pitchviz.routes', ['ngRoute']); 

    angular 
    .module('pitchviz') 
    .run(run); 

    run.$inject = ['$http']; 

    function run($http) { 
    $http.defaults.xsrfHeaderName = 'X-CSRFToken'; 
    $http.defaults.xsrfCookieName = 'csrftoken'; 
    } 
})(); 

pitchviz.config.js :

(function() { 
'use strict'; 

    angular 
    .module('pitchviz.config') 
    .config(config); 

    config.$inject = ['$locationProvider']; 

    function config($locationProvider) { 
    $locationProvider.html5Mode(true); 
    $locationProvider.hashPrefix('!'); 
    } 
})(); 

pitchviz.routes.js :

(function() { 
    'use strict'; 

    angular 
    .module('pitchviz.routes') 
    .config(config); 

    config.$inject = ['$routeProvider']; 

    function config($routeProvider) { 
    $routeProvider.when('/', { 
     controller: 'ReportTypeController', 
     controllerAs: 'vm', 
     templateUrl: '/static/templates/report-types/report-types.html' 
    }).otherwise('/'); 
    } 
})(); 

보고서 - 유형/보고서 -types.modules.js :

(function() { 
    'use strict'; 

    angular 
    .module('pitchviz.report-types', [ 
     'pitchviz.report-types.controllers', 
     'pitchviz.report-types.services' 
    ]); 

    angular 
    .module('pitchviz.report-types.controllers', []); 

    angular 
    .module('pitchviz.report-types.services', []); 
})(); 

보고서 - 유형/컨트롤러/보고서 - type.controller.js :

(function() { 
    'use strict'; 

    angular 
    .module('pitchviz.report-types.controllers') 
    .controller('ReportTypeController', ['reportTypeService', ReportTypeController]); 

    ReportTypeController.$inject = ['reportTypeService']; 

    function ReportTypeController(reportTypeService) { 
    var vm = this; 

    vm.reportType = undefined; 

    activate(); 

    function activate() { 

     reportTypeService.get().then(reportTypeSuccess, reportTypeError); 

     function accountSuccess(data, status, headers, config) { 
     console.log(data); 
     vm.reportType = data.data; 
     } 

     function reportTypeError(data, status, headers, config) { 
     console.log(data); 
     console.log(status); 
     } 
    } 
    } 
})(); 

보고서 형/서비스/보고서 - type.service.js는 :

(function() { 
    'use strict'; 

    angular 
    .module('pitchviz.report-types.services') 
    .service('reportTypeService', ['reportTypeService', reportTypeService]); 

    reportTypeService.$inject = ['$http']; 

    function reportTypeService($http) { 
    var reportTypeService = { 
     get: get 
    }; 

    return reportTypeService; 

    function get() { 
     console.log("service"); 
     return $http.get('/api/report-types/'); 
    } 
    } 
})(); 

여기 무슨 일이야? 감사!

+2

가 음, 어디 .... 워, 여기 구문은 매우 혼란 스럽다.$ inject를 사용하려면 $ inject 만 사용하고 배열을 통해 주입하지 마십시오 (예 : https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md# 데이터 분리 호출). 여기에 무슨 일이 일어나고 있는지 이야기하기가 어렵습니다. reportTypeService를 reportTypeService에 삽입하려고하는 것처럼 보입니다. 이해가되지 않습니다. –

+0

생성 한 서비스, 컨트롤러 등은 index.html 파일에 주입했습니다.이 유형의 오류는 일반적으로 각도가 삽입중인 종속성 중 하나를 찾을 수없는 경우에 발생합니다. – pritesh

+0

'.service ('reportTypeService', [ 'reportTypeService', reportTypeService]); reportTypeService. $ inject = [ '$ http'];'좋지 않습니다. 그 중 하나에 붙이면 (그리고 btw, 인라인 주석이 잘못됨). – estus

답변

0

파일 당 하나의 모듈이 단색 패턴이지만 여기에서 잘못 사용되는 것이 문제입니다. 하위 모듈은 파일에 정의되어야합니다. report-type.controller.js :

angular 
    .module('pitchviz.report-types.controllers', []) 
    .controller('ReportTypeController', ...) 

상위 모듈 파일에는 report-types.modules.js가 없습니다.

모듈 만 있으면 모듈 단위가 있고 모듈 파일은 임의의 순서로로드 될 수 있습니다.

0

reportTypeServicepitchviz.report-types.services 모듈. 그러나 ReportTypeControllerpitchviz.report-types.controllers 모듈입니다. 나는 pitchviz.report-types.controllers이 별도의 모듈이기 때문에 reportTypeService을 식별 할 수 없다고 생각한다. pitchviz.report-types.controllers 모듈에 reportTypeService에 액세스하려는 경우 reportTypeServicepitchviz.report-types.controllers 모듈에 추가하거나 pitchviz.report-types.services 모듈을 pitchviz.report-types.controllers 모듈에 삽입해야합니다.

angular.module("pitchviz.report-types.controllers", ["pitchviz.report-types.services"]); 
+0

아닙니다. pitchviz.report-types가 주 모듈에로드되면 reportTypeService 모듈이 어느 모듈에 속해 있는지 상관 없습니다. 여기서는 오류가 발생하지 않습니다. 그러나 보여준대로 모듈 종속성을 정의하는 것이 유용합니다. 그렇게하는 것이 좋습니다. – estus

0

관측 : 당신이 거기 confusable 구문을 만들고 여기에, 문제가 report-types/controllers/report-type.controller.js입니다.

  • 배열을 사용하여 이미 삽입 했으므로 $inject을 사용하여 서비스를 삽입 할 필요가 없습니다.

이 제거 ReportTypeController.$inject = ['reportTypeService'];

단순히이 사용

.controller('ReportTypeController', ['reportTypeService', ReportTypeController]); 

function ReportTypeController(reportTypeService) { 
    .... 
    .... 
    .... 
} 
관련 문제