Skip to content

Instantly share code, notes, and snippets.

@leocaseiro
Created May 15, 2015 01:27
Show Gist options
  • Save leocaseiro/5dc6ae2461f7d9401ba9 to your computer and use it in GitHub Desktop.
Save leocaseiro/5dc6ae2461f7d9401ba9 to your computer and use it in GitHub Desktop.
Angular Create Custom Directive
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<title>Angular Create Directive</title>
</head>
<body>
<div ng-controller="Example">
<button click="click()">click here</button>
<clicker go-click="click()">I am a transcluder</clicker>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js"></script>
<script>
angular.module('MyApp', [])
.directive('click', function(){
return {
restrict: 'A',
link: function(scope, element, attrs) {
console.log('click>attrs:', attrs);
element.bind('click', function(){
scope.$eval(attrs.click);
});
}
};
})
.directive('clicker', function(){
return {
restrict: 'E',
transclude: true,
template: '<button ng-transclude></button>',
link: function(scope, element, attrs) {
console.log('clicker>attrs:', attrs);
element.bind('click', function(){
scope.$eval(attrs.goClick);
});
}
};
})
.controller('Example', function($scope){
$scope.message = 'testing my directive';
$scope.click = function() {
alert($scope.message);
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment