Created
June 29, 2015 19:37
-
-
Save maxpeterson/4d5d377b09c832b43de5 to your computer and use it in GitHub Desktop.
AngularJs - bind the local variable name to the remote variable name in the parent scope.
This file contains hidden or 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
| // Bind the local variable name to the remote variable name in the parent scope. | |
| module.factory('twoWayBind', function ($parse) { | |
| return function (scope, local, remote) { | |
| // 2-way bind the model attribute | |
| if (angular.isUndefined(remote)) { | |
| remote = local; | |
| } | |
| var parentAssign = $parse(remote).assign; | |
| var localAssign = $parse(local).assign; | |
| scope.$watch(remote, function(value) { | |
| localAssign(scope, value); | |
| }); | |
| scope.$watch(local, function(value) { | |
| parentAssign(scope.$parent, value); | |
| }); | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment