programing

프로바이더에서 $window에 접속하는 방법

sourcejob 2023. 3. 15. 19:33
반응형

프로바이더에서 $window에 접속하는 방법

접속하고 싶다$window(프로바이더로부터)mymodule.provider(...), 설정 시 이러한 의존성이 필요합니다(따라서 이러한 의존성을 new from the configure time에 삽입해도 소용없습니다).$get).

현재 액세스 할 수 없습니다.$window설정 시간에 이러한 의존성을 필요로 하는 이유는, 에 액세스 할 필요가 있기 때문입니다.$window.STATIC_URL이 값은 창의 상수이며, 이 값을 사용하여 주의 템플릿을 설정합니다($stateProvider).

현재 코드:

mymodule.provider('StaticUrl', function() {
    this.url = function() { return window.STATIC_URL || 'static'; };
    this.$get = this.url;
});

이는 설정 및 실행 시간 모두에서 이러한 값이 필요하기 때문입니다(이 값은 단순히 상태를 설정하는 데 그치지 않고 사용하기 때문에 컨트롤러에 이러한 URL이 필요한 경우 종속성으로서 Import합니다).

//config
mymodule.config(['StaticUrlProvider', '$stateProvider', function(sup, $sp){
    //...
    $sp.state('main', {
        templateUrl: sup.url() + 'path/to/template.html'
    });
    //...
}]);
//run-time (in this case: a controller)
mymodule.controller('MyController', ['$scope', 'StaticUrl', function($s, su) {
    var buildingType = /*a complex expression here*/;
    $s.buildingPicture = $su + 'path/to/building/pics/' + buildingType + '.png';
}]);

기존 데이터 세트를 지정하면 STATIC_URL 아래에 각 엔트리에 대한 그림이 있으며, 이 그림을 제공해야 합니다.즉, 두 단계에서 상수를 사용합니다.

더 Angular-way(글로벌에 의존하지 않고)에서 어떻게 해야 합니까?접속할 수 있는 방법이 있나요?$window프로바이더로부터, 프로바이더로부터, 인스톨을 개시하지 않고moduleerr?

주입$windowProvider및 콜$windowProvider.$get()

언급URL : https://stackoverflow.com/questions/26490102/how-to-access-window-in-a-provider

반응형