Created
March 21, 2016 15:51
-
-
Save vespertilian/cfbe6cbe04ca60b5d658 to your computer and use it in GitHub Desktop.
Retrofit bindToController for Angular 1.2 Typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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