【发布时间】:2016-12-14 00:36:03
【问题描述】:
我正在使用角度路线。 基于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: '/'
});
}
]);
我的网址: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;
}
完整的打字稿代码:http://codepen.io/alexmallinger/pen/qNQRGJ
编辑:将static $inject: string[] = ['$routeParams']; 行添加到我的代码中
【问题讨论】:
标签: angularjs typescript angular-routing