config() 모듈에서의 의존관계 주입: 각도JS
현재 app.js에는 다음과 같은 루트가 있습니다.
var gm = angular.module('gm', ['gm.services','gm.directives','gm.filters','gm.controllers','ngSanitize']);
gm.config(['$routeProvider', 'Path', function($routeProvider, Path) {
$routeProvider.when('/login', {
templateUrl: Path.view('application/authentication/login.html'),
controller: 'authController'
});
$routeProvider.when('/dashboard', {
templateUrl: Path.view('application/dashboard/index.html'),
controller: 'dashboardController'
});
$routeProvider.otherwise({
redirectTo: '/login'
});
}]);
보시다시피 경로 의존성을 주입하려고 합니다.이 공급자를 찾을 수 없다는 오류가 표시되지만,이것은 구성 모듈 공급자가 가장 먼저 실행되기 때문이라고 생각합니다.아래는 "services.js"에 있는 경로 공급자의 정의입니다.
gm.factory("Path", function() {
return {
view: function(path) {
return 'app/views/' + path;
},
css: function(path) {
return 'app/views/' + path;
},
font: function(path) {
return 'app/views/' + path;
},
img: function(path) {
return 'app/views/' + path;
},
js: function(path) {
return 'app/views/' + path;
},
vendor: function(path) {
return 'app/views/' + path;
},
base: function(path) {
return '/' + path;
}
}
});
이 프로바이더를 설정 모듈에 삽입하려면 어떻게 해야 합니까?
angular.config
프로바이더만 받아들입니다.- 모든 서비스, 공장 등은 프로바이더의 인스턴스입니다.
따라서 서비스를 구성에 삽입하려면 서비스 이름에 '공급자'를 추가하여 서비스 공급자에게 전화하면 됩니다.
angular.module('myApp')
.service('FooService', function(){
//...etc
})
.config(function(FooServiceProvider){
//...etc
});
angularjs Provider 문서에 따라
... 공장 출하 시 레시피를 정의하면 빈 공급자 타입에
$get
출고 시 기능으로 설정된 메서드는 후드 아래에 자동으로 생성됩니다.
다음과 같은 공장(또는 서비스)이 있는 경우:
.factory('myConfig', function(){
return {
hello: function(msg){
console.log('hello ' + msg)
}
}
})
먼저 다음 명령어를 사용하여 공장을 호출해야 합니다.$get
method는 반환된 오브젝트에 액세스하기 전에 다음과 같이 입력합니다.
.config(function(myConfigProvider){
myConfigProvider
.$get()
.hello('world');
});
인.config
프로바이더만 사용할 수 있습니다(예:$routeProvider
)에 입력되어 있습니다..run
서비스 인스턴스만 사용할 수 있습니다(예:$route
공급자가 아닌 공장이 있습니다.이 스니펫을 참조해 주세요.이 스니펫의 작성 방법은 다음과 같습니다. 서비스, 공장 및 프로바이더 각 문서(https://docs.angularjs.org/guide/services)에서도 이 점에 대해 언급하고 있습니다.
이 경우 constant를 사용해야 합니다.이것은 프로바이더 이외의 설정 단계에서 주입할 수 있는 유일한 것이기 때문입니다.
angular.module("yourModule").constant("paths", {
base: function(){ ... }
});
이 토론은 제가 같은 것을 알아내려고 할 때 도움이 되었습니다.
$routeProvider.when('/', {
templateUrl:'views/main.html',
controller:'MainController',
resolve: {
recentPosts: ['$q', 'backendService', function($q, backendService){
var deferred = $q.defer();
backendService.getRecentPosts().then(
function(data) {
var result = data.result;
deferred.resolve(result);
},
function(error) {
deferred.reject(error);
}
);
return deferred.promise;
}]
}
})
언급URL : https://stackoverflow.com/questions/17485900/injecting-dependencies-in-config-modules-angularjs
'it-source' 카테고리의 다른 글
Spring Boot : 데이터베이스에서 @Scheduled cron 값을 가져옵니다. (0) | 2023.03.17 |
---|---|
컨트롤러 함수에 대한 호출이 기능하지 않는 각도 ng클릭 (0) | 2023.03.17 |
Spring Boot 및 Logback을 사용하여 콘솔에 대한 휴지 상태 로깅을 피할 수 없음 (0) | 2023.03.17 |
React-Router와의 액티브링크 (0) | 2023.03.17 |
유형 설명: 모듈 외부에서 가져오기 문을 사용할 수 없습니다. (0) | 2023.03.17 |