AngularJS Typescript IRouteParamsService undefined -
i´am using angular-route. based on this question i´ve tried url parameter.
i following error: angular.js:13920 typeerror: cannot read property 'zvar' of undefined
seems $routeparams
undefined.
my routing config: 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: '/' }); } ]);
my url: http://localhost:64025/#/calc?zvar=test123
the access in constructor:
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; }
full typescript code: http://codepen.io/alexmallinger/pen/qnqrgj
edit: added line static $inject: string[] = ['$routeparams'];
code
you should inject $routeparams
service. $inject property annotation can used this:
export class calcctrl implements icontroller { static $inject:string[] = ['$routeparams']; constructor(private $routeparams: irouteparams) { ... } }
Comments
Post a Comment