AngularJS 打字稿 IRouteParamsService 未定义
AngularJS Typescript IRouteParamsService undefined
我正在使用 angular-路线。
基于 this question 我尝试获取 URL 参数。
我收到以下错误:
angular.js:13920 TypeError: Cannot read property 'zvar' of undefined
似乎 $routeParams
未定义。
我的路由配置:
angular.module('app').config(['$routeProvider',
function routes($routeProvider: ng.route.IRouteProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/start.html',
})
.when('/calc/:zvar?', {
templateUrl: 'views/calc.html',
controller: 'app.controllers.calcCtrl'
})
.otherwise({
redirectTo: '/'
});
}
]);
我的URL:http://localhost:64025/#/calc?zvar=test123
构造函数中的访问:
interface IRouteParams extends ng.route.IRouteParamsService {
zvar: string;
}
export class calcCtrl implements IController {
static $inject: string[] = ['$routeParams'];
constructor(private $routeParams: IRouteParams) {
this.zvar = $routeParams.zvar;
console.log("Calc Controller: " + this.zvar);
}
zvar: string;
}
完整的 Typescript 代码:http://codepen.io/alexmallinger/pen/qNQRGJ
编辑:将行 static $inject: string[] = ['$routeParams'];
添加到我的代码
您应该注入 $routeParams
服务。 $inject property annotation 可用于此:
export class calcCtrl implements IController {
static $inject:string[] = ['$routeParams'];
constructor(private $routeParams: IRouteParams) {
...
}
}
我正在使用 angular-路线。 基于 this question 我尝试获取 URL 参数。
我收到以下错误:
angular.js:13920 TypeError: Cannot read property 'zvar' of undefined
似乎 $routeParams
未定义。
我的路由配置:
angular.module('app').config(['$routeProvider',
function routes($routeProvider: ng.route.IRouteProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/start.html',
})
.when('/calc/:zvar?', {
templateUrl: 'views/calc.html',
controller: 'app.controllers.calcCtrl'
})
.otherwise({
redirectTo: '/'
});
}
]);
我的URL:http://localhost:64025/#/calc?zvar=test123
构造函数中的访问:
interface IRouteParams extends ng.route.IRouteParamsService {
zvar: string;
}
export class calcCtrl implements IController {
static $inject: string[] = ['$routeParams'];
constructor(private $routeParams: IRouteParams) {
this.zvar = $routeParams.zvar;
console.log("Calc Controller: " + this.zvar);
}
zvar: string;
}
完整的 Typescript 代码:http://codepen.io/alexmallinger/pen/qNQRGJ
编辑:将行 static $inject: string[] = ['$routeParams'];
添加到我的代码
您应该注入 $routeParams
服务。 $inject property annotation 可用于此:
export class calcCtrl implements IController {
static $inject:string[] = ['$routeParams'];
constructor(private $routeParams: IRouteParams) {
...
}
}