Skip to content

Instantly share code, notes, and snippets.

@btford btford/example.js
Created Oct 31, 2014

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

prokoptsev commented Jan 14, 2018

bindToController doesn't return ddo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.