Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angularjs+;Typescript,如何将$routeParams与IRouteParamsService一起使用_Javascript_Angularjs_Typescript_Angularjs Routing - Fatal编程技术网

Javascript Angularjs+;Typescript,如何将$routeParams与IRouteParamsService一起使用

Javascript Angularjs+;Typescript,如何将$routeParams与IRouteParamsService一起使用,javascript,angularjs,typescript,angularjs-routing,Javascript,Angularjs,Typescript,Angularjs Routing,我使用$routeParams从URI中提取属性,并为它们设置本地变量 当我使用typescripts键入设置$routeParams的类型时,我无法让日志记录器访问$routeParams 如何访问$routeParams中的属性 class Controller{ constructor($routeParams: ng.route.IRouteParamsService, private $location: ng.ILocationService) this.pr

我使用$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代码为:

interface IRouteParamsService {
    [key: string]: any;
}
这有一个错误:ng.route.IRouteParamsService类型上不存在属性“property1”

如果我将$routeParams的类型更改为:any,那么它将正确设置property1。
如何在仍然访问$routeParams中的属性的情况下保持Typescript的严格键入?

解决了这个问题。您需要声明一个使用IRoutParamsService的接口,并指定要使用的属性

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");
    }
 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");
    }
}  

请注意控制器构造函数中的更改。

您可以使用
$routeParams['property1']
而不是
$routeParams.property1