Skip to content

Instantly share code, notes, and snippets.

@elegantcoder
Created February 12, 2014 09:42
Show Gist options
  • Save elegantcoder/8952564 to your computer and use it in GitHub Desktop.
Save elegantcoder/8952564 to your computer and use it in GitHub Desktop.
// watch - translate - assign other member of scope
// check out how it works at http://jsfiddle.net/SWe4r/1/
var app = angular.module('watcherApp', []);
app.controller('watcherController', function ($scope) {
$scope.source = 'aaaaaa';
$scope.foo = {
bar: 'aaa'
};
$scope.bar = {
foo: ''
}
$scope.translate = function (src) {
return src.replace(/a/g, 'b');
}
});
app.directive('watcher', function ($parse) {
return {
scope: {
source: '@',
target: '@',
translator: '@'
},
transclue: true,
restrict: 'E',
link: function (scope, element, attr) {
var translate = $parse(scope.translator)(scope.$parent);
var setter = $parse(scope.target).assign;
var getter = $parse(scope.source);
scope.$parent.$watch(scope.source, function () {
setter(scope.$parent, translate(getter(scope.$parent)))
});
}
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment