Created
February 12, 2014 09:42
-
-
Save elegantcoder/8952564 to your computer and use it in GitHub Desktop.
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
// 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