Skip to content

Instantly share code, notes, and snippets.

@jonathanwork
Created May 4, 2016 05:55
Show Gist options
  • Save jonathanwork/e1d0341eea6f81b910c25b9284294de1 to your computer and use it in GitHub Desktop.
Save jonathanwork/e1d0341eea6f81b910c25b9284294de1 to your computer and use it in GitHub Desktop.
simple angular event directive
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Angular Focus Event Directive</title>
</head>
<body>
<div ng-controller="myController">
<h2>Focus Event Directive</h2>
Input 1: <br>
<input type="text"
ng-blur="focusLost($event, 'input1')"
ng-focus="focusGained('input1')"> <br>
Input 2: <br>
<input type="text"
ng-blur="focusLost($event, 'input2')"
ng-focus="focusGained('input2')"> <hr>
Input Data: {{inputData|json}} <br
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script src="./directive_focus_events.js"></script>
</body>
</html>
angular.module('myApp', []).
controller('myController', function($scope) {
$scope.inputData = { input1: '',
input2: ''};
$scope.focusGained = function(input) {
$scope.inputData[input] = '';
}
$scope.focusLost = function (event, input) {
var element = angular.element(event.target);
var value = element.val();
$scope.inputData[input] = value.toUpperCase();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment