2014-06-19 2 views
9

$ routeParams를 사용하여 URI에서 속성을 가져 와서 로컬 변수를 설정합니다.

typescripts 입력을 사용하여 $ routeParams 유형을 설정하면 $ routeParams에 액세스 할 수 없습니다.

어떻게 $ routeParams의 속성에 액세스 할 수 있습니까?

class Controller{ 
    constructor($routeParams: ng.route.IRouteParamsService, private $location: ng.ILocationService) 
     this.propetry1 = this.getProperty1($routeParams.property1); 
    } 

    property1: string; 

    getProperty1(input: string):string{ 
     return (input || "Not present"); 
    } 

}

ng.route.IRouteParamsService 코드는이 : 재산

'속성 1이 ng.route.IRouteParamsService의 종류에 존재하지 않는'

interface IRouteParamsService { 
    [key: string]: any; 
} 

이것은 오류가

$ routeParams의 유형을 : any로 변경하면 property1이 올바르게 설정됩니다. $ routeParams의 속성을 계속 처리하면서 Typescript의 엄격한 입력을 유지하려면 어떻게해야합니까?

답변

23

알아 냈어. IRouteParamsService를 사용하는 인터페이스를 선언하고 사용할 속성을 지정해야합니다.

interface IRouteParams extends ng.route.IRouteParamsService { 
    property1:string; 
} 

class Controller{ 
    constructor($routeParams: IRouteParams, private $location: ng.ILocationService) 
     this.propetry1 = this.getProperty1($routeParams.property1); 
    } 

    property1: string; 

    getProperty1(input: string):string{ 
     return (input || "Not present"); 
    } 
} 

컨트롤러의 컨스트럭터가 변경된 것에 주목하십시오.

2

당신은 $routeParams['property1'] 대신 $routeParams.property1

로 사용할 수 있습니다
관련 문제