Skip to content

Instantly share code, notes, and snippets.

@btford
Created October 31, 2014 19:45
Show Gist options
  • Save btford/00174ce864eed5ecbae4 to your computer and use it in GitHub Desktop.
Save btford/00174ce864eed5ecbae4 to your computer and use it in GitHub Desktop.
bindToController in ng 1.2.x
// this example shows how to write a helper to get something like bindToController
// in angular 1.2.x
// this example is incomplete and mostly meant to illustrate the technique
angular.module('myModule', []).directive('myDir', function () {
return bindToController({
scope: {
foo: '='
},
controller: 'MyController',
controllerAs: 'myCtrl'
});
});
// this isn't complete, but it is a good starting point.
// for isntance, it doesn't really handle all types of bindings.
function bindToController (ddo) {
var link = ddo.link || angular.noop;
ddo.link = function (scope, element, attrs, ctrls) {
// watch $scope.foo and copy changes to $scope.myCtrl.foo
Object.keys(ddo.scope).forEach(function (prop) {
scope.$watch(prop, function (newVal) {
scope[ddo.controllerAs][prop] = newVal;
});
});
// run original link fn
return link(scope, element, attrs, ctrls);
};
}
@prokoptsev
Copy link

bindToController doesn't return ddo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment