Skip to content

Instantly share code, notes, and snippets.

@umidjons
Created September 23, 2013 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umidjons/6669708 to your computer and use it in GitHub Desktop.
Save umidjons/6669708 to your computer and use it in GitHub Desktop.
AngularJS Custom Directive Example
<!doctype html>
<html lang="en-US" data-ng-app="app">
<head>
<meta charset="UTF-8">
<title>AngularJS Custom Directive</title>
<script type="text/javascript" src="js/angular.js"></script>
</head>
<body data-ng-controller="SomeController">
<button data-ng-click="clickUnfocused()">Not Focused</button>
<button data-ng-click="clickFocused()" ngbk-focus>I'm very Focused!</button>
<div>{{message.text}}</div>
<script type="text/javascript">
var myModule = angular.module( 'app', [] )
.directive( 'ngbkFocus', function ()
{
return {
link: function ( scope, element, attrs, controller )
{
element[0].focus();
}
}
} )
.controller( 'SomeController', function ( $scope )
{
$scope.message = {text: 'Nothing clicked'};
$scope.clickUnfocused = function ()
{
$scope.message.text = 'Unfocused button clicked';
}
$scope.clickFocused = function ()
{
$scope.message.text = 'Focused button clicked';
}
} );
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment