Last active
December 17, 2015 01:10
-
-
Save boneskull/5526758 to your computer and use it in GitHub Desktop.
This shows how you can use arbitrary attributes within a directive.
This file contains 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
<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