I am Very new to typescript and angular Js . I have am not able to find correct answer , my code is given below:
export class SidenavController {
static $inject = ['$scope', '$mdSidenav'];
constructor(private $scope: any,private $mdSidenav: any) {
}
toggleSidenav(name: string) {
this.$mdSidenav(name).toggle();
}
loadHelpInfo() {
this.helpService.loadAll()
.then(function(help) {
this.$scope.helpInfo = [].concat(help);
this.$scope.selected = this.$scope.helpInfo[0];
})
}
selectHelpInfo(help) {
this.$scope.selected = angular.isNumber(help) ? this.$scope.helpInfo[help] : help;
this.$scope.toggleSidenav('left');
}
}
app.service('helpService', ['$q', function($q) {
var helpInfo = [{
name: 'Introduction',
content: '1 '
}, {
name: 'Glossary',
content: '2'
}, {
name: 'Log In',
content: '3'
}, {
name: 'Home Page',
content: '4'
}];
return {
loadAll: function() {
return $q.when(helpInfo);
}
};
}]);
In the above code I want to use helpService to load the details on the screen. I am getting following error while executing : app/components/sidenav/sidenav-controller.ts(10,10): error TS2339: Property 'helpService' does not exist on type 'SidenavController'. I am not sure how to use services in typescript. Also if required I have done codepen version of the angular :
http://codepen.io/snehav27/pen/JdNvBV
Basically I am trying to do typescript version of above snippet
You need to inject
helpservice
and set it in your constructor argument.otherwise Typescript does not know what are you referring to as
this.helpService
, even without TS it will result in an error when you try to dothis.helpService.loadAll
with "cannot access loadAll of undefined" error or similar.Also use Arrow operator to resolve lexical scoped
this
@otherwise it will result in another error since
this
wont be the controller instance inside the callback.You may also create a typing for
helpService
for better usage and reduce any typos.and
It would also be better to use controller as syntax with Typescript class defn and totally get rid of attaching properties to scope. What you have right now is half baked (functions attached to controller instance and some properties to scope).
and in your view:
and refer to the properties and methods prefixed with
vm
(You could use any alias, i just usevm
). Example (you should be able to figure out rest):-