Skip to content

Instantly share code, notes, and snippets.

@vespertilian
Created March 21, 2016 15:51
Show Gist options
  • Save vespertilian/cfbe6cbe04ca60b5d658 to your computer and use it in GitHub Desktop.
Save vespertilian/cfbe6cbe04ca60b5d658 to your computer and use it in GitHub Desktop.
Retrofit bindToController for Angular 1.2 Typescript
export function infoChart() {
return {
restrict: 'A',
scope: {
highchartsOptions: '=',
title: '@',
info: '@'
},
templateUrl: 'template.html',
controllerAs: 'vm',
controller: InfoChartController,
bindToController: true
};
}
export class InfoChartController {
title: string;
constructor($scope: ng.IScope){
this.oneWayBind($scope, 'title');
this.twoWayBind($scope, 'info');
this.oneWayBind($scope, 'highchartsOptions');
}
oneWayBind($scope: ng.IScope, keyToBind: string){
let unbindWatcher = $scope.$watch(keyToBind, (newValue)=> {
if(typeof(newValue) !== 'undefined'){
this[keyToBind] = newValue;
// unbind as soon as value is not undefined so we dont grow the $watch count
unbindWatcher();
}
})
}
twoWayBind($scope: ng.IScope, keyToBind: string){
$scope.$watch(keyToBind, (newValue)=> {
if(typeof(newValue) !== 'undefined'){
this[keyToBind] = newValue;
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment