Created
May 15, 2015 01:27
-
-
Save leocaseiro/5dc6ae2461f7d9401ba9 to your computer and use it in GitHub Desktop.
Angular Create Custom Directive
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
<!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