I recently started using bindToController in my angular directives and I had an issue with 'this'. In my controller method, how to I access properties of the class MultiSelect. 'this' in that context is referring to the $scope due to the controllerAs syntax which is expected, but now how do I access my searchService service?
/// <reference path="../../../../definitions/app.d.ts" />
module App.directives
{
'use strict';
class MultiSelect implements ng.IDirective
{
restrict = 'E';
templateUrl = 'directives/multi-select/multi-select.directive.html';
scope = {};
bindToController = {
value: '='
};
controllerAs = 'multiSelect';
constructor(private searchService: App.ISearchService) {
}
controller()
{
console.log(this)
// prints {value: undefined}
// which matches bindToController
this.searchService.get();
// TypeError: Cannot read property 'get' of undefined
}
link = ($scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) => {
}
static factory(): ng.IDirectiveFactory
{
const directive = (searchService: App.ISearchService) => new MultiSelect(searchService);
return directive;
}
}
angular.module('App').directive('multiSelect', ['searchService', MultiSelect.factory()]);
}