Skip to content

Instantly share code, notes, and snippets.

@dcherman
Last active March 4, 2017 16:00
Show Gist options
  • Save dcherman/57621dd904cf598f12b81ae6e0f4b6b2 to your computer and use it in GitHub Desktop.
Save dcherman/57621dd904cf598f12b81ae6e0f4b6b2 to your computer and use it in GitHub Desktop.
angular.module('ngBindonPolyfill', [['$provide', '$compileProvider', function($provide, $compileProvider) {
$compileProvider.component('ngBindonSupportTest', {
controller: ['$attrs', function($attrs) {
this.a = $attrs;
}],
bindings: {
x: '<',
xChanged: '&?'
}
});
$provide.decorator('$compile', ['$delegate', '$rootScope', function($delegate, $rootScope) {
var testScope = $rootScope.$new();
var elem = angular.element('<ng-bindon-support-test ng-bindon-x="x">');
$delegate(elem)(testScope);
var $ctrl = elem.controller('ngBindonSupportTest');
testScope.$destroy();
elem.remove();
var ngBindonAttr = /^ngBindon[A-Z]/;
if (!$ctrl.hasOwnProperty('xChanged')) {
Object.defineProperty(Object.getPrototypeOf($ctrl.a), '$$element', {
enumerable: true,
get: function() {
return this.$$e;
},
set: function(value) {
var node = value && value[0];
if (node) {
for (var name in this) {
if (ngBindonAttr.test(name)) {
var prop = name.substr(8);
prop = prop.charAt(0).toLowerCase() + prop.slice(1);
this[prop] = this[name];
this[prop + 'Changed'] = this[name] + '=$event';
}
}
}
return (this.$$e = value);
}
});
}
return $delegate;
}]);
}]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment