Skip to content

Instantly share code, notes, and snippets.

@maxpeterson
Created June 29, 2015 19:37
Show Gist options
  • Select an option

  • Save maxpeterson/4d5d377b09c832b43de5 to your computer and use it in GitHub Desktop.

Select an option

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.
// 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