Skip to content

Instantly share code, notes, and snippets.

@boneskull
Last active December 17, 2015 01:10
Show Gist options
  • Save boneskull/5526758 to your computer and use it in GitHub Desktop.
Save boneskull/5526758 to your computer and use it in GitHub Desktop.
This shows how you can use arbitrary attributes within a directive.
<html>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.min.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.name = 'Superhero';
}
myApp.directive('anything', function() {
return {
restrict: 'E',
link: function(scope, elm, attrs) {
angular.forEach(attrs, function(value, attr) {
if (attr.charAt(0) !== '$') {
attrs.$observe(attr, function(val) {
console.log(attr + ' => ' + angular.toJson(scope.$eval(val)));
});
}
});
}
}
});
</script>
<body ng-app="myApp">
<div ng-controller="MyCtrl">
<anything foo="'{{name}}'" bar="'duh'" baz="{spam: 'frogs'}"></anything>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment