Created
October 31, 2014 19:45
-
-
Save btford/00174ce864eed5ecbae4 to your computer and use it in GitHub Desktop.
bindToController in ng 1.2.x
This file contains 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
// 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); | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bindToController
doesn't returnddo