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); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
prokoptsev commentedJan 14, 2018
bindToControllerdoesn't returnddo