Skip to content

Instantly share code, notes, and snippets.

@thomasboyt
Created June 13, 2013 16:11
Show Gist options
  • Save thomasboyt/5774991 to your computer and use it in GitHub Desktop.
Save thomasboyt/5774991 to your computer and use it in GitHub Desktop.
angular bindAttr
module.directive("bindAttr", function() {
return function(scope, element, attrs) {
var args = JSON.parse(attrs.bindAttr);
for (var key in args) {
scope.$watch(args[key], function() {
var val = scope.$eval(args[key]);
attrs.$set(key, val);
}, true);
}
}
});
@thomasboyt
Copy link
Author

example usage:

<div bind-attr='{"data-my-weird-attribute": "myWeirdAttributeValue()"}'>
$scope.myWeirdAttributeValue = function() {
  // ...
  return "foo"
}

Results in

<div data-my-weird-attribute="foo" bind-attr='{"data-my-weird-attribute": "myWeirdAttributeValue()"}`>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment