Skip to content

Instantly share code, notes, and snippets.

@danielpsf
Last active August 29, 2015 13:55
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 danielpsf/8786645 to your computer and use it in GitHub Desktop.
Save danielpsf/8786645 to your computer and use it in GitHub Desktop.
Just to understand more about binding an event watch to some element into a directive. you can see here: http://jsbin.com/EluKodAC/3
angular.module('myApp', [])
.controller('myCtrl', ['$scope', function($scope) {
$scope.MyProccessedData = "";
}])
.directive('myDirective', [function(){
return function(scope, element, attrs) {
element.bind('keydown', function(event) {
scope.MyProccessedData = scope.$eval(attrs.ngModel);
});
};
}]);
<!DOCTYPE html>
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"></script>
<script src="app.js"></script>
<meta charset=utf-8 />
<title>Watching my input</title>
</head>
<body data-ng-app="myApp" data-ng-controller="myCtrl">
<label>
Type something here:
<input type="text" data-ng-model="myModel" data-my-directive />
</label>
<p>{{input}}</p>
<p>Unprocessed data: {{myModel}}</p>
<p>Processed data: {{MyProccessedData}}</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment