2014-12-02 5 views
2

서비스 공급자를 확장하려고합니다. 나는이 스레드를 읽은서비스 공급자 (공급자) 확장

angular 
    .module('myapp') 
    .config(Routes); 

function Routes(customRouteProvider, $routeProvider) { 

    /* This will print an object that looks the same as 
     the ones printed in the provider definition */ 
    console.log($routeProvider); 

    /* This object does not look like the $routeProvider any more :(*/ 
    console.log(customRouteProvider); 

    customRouteProvider 
     .when('/', { 
      templateUrl: 'path/to/some/template.html', 
      controller: 'SomeController', 
     }) 
     .otherwise({ 
      redirectTo: '/' 
     }); 
} 

:

angular 
    .module('myapp') 
    .provider('customRoute', customRoute) 

function customRoute($routeProvider) { 

    var extendedProvider = angular.extend({}, $routeProvider, { 
     // Customization here 
    }); 

    // These will print almost identical objects 
    console.log(extendedProvider); 
    console.log($routeProvider); 

    this.$get = function() { 
     return extendedProvider; 
    } 
} 

노선의 설정은 다음과 같은 : 더 구체적으로

내 확장 된 공급자가이처럼 보이는 ngRoute 모듈에서 routeProvider을 $ :

how can i extend a service

그러나 오 "공장"서비스 확장에 대해 이야기합니다.

누군가 여기서 일어나는 일을 설명 할 수 있습니까? 도움을 많이 주시면 감사하겠습니다.

답변

1

확장 바이더 객체 반환해야 $routeProvider 당신의 customRoute 기능을 확장하기 위해 :

customRouteProvider 
    .when('/', { 
     templateUrl: 'path/to/some/template.html', 
     controller: 'SomeController', 
    }) 
    .otherwise({ 
     redirectTo: '/' 
    }) 
    .mergeRoutes({ ... }); 

데모 재생하려면, 당신은 예를 들어 당신이 새 사용자 지정 방법 customRouteProvider.mergeRoutes 수있는 후

function customRoute($routeProvider) { 

    var extendedProvider = angular.extend({}, $routeProvider, { 
     // Customization here 
     mergeRoutes: function() {} 
    }); 

    return extendedProvider; 
} 

를 : http://plnkr.co/edit/mht94RFLScz2jHCwPcai?p=preview

당신이 this.$get을 엉망으로 만들 필요가 없다면, $routeProvider에 몇 가지 새로운 방법을 추가하기 만하면됩니다. this.$get 해당 공급자 서비스 ($routeProvider의 경우 $route)에 대한 서비스 개체 인스턴스/생성자를 반환합니다.