AngularJS Tutorial - Custom directive with attribute








The following code shows how to create Custom directive with attribute.

Example


<!--from www  .j a va  2  s .  co m-->
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
var app = angular.module('myApp', []);
app.directive('yourInput', function() {
    return {
        restrict: 'E',
        scope: {
            form: '='
        },
        template: '<span>Hello World</span>',
        link: function(scope, element, attrs) {
            console.log(scope.form);
        }
    };
});
</script>
</head>
<body>
  <p><em>Check the console</em></p>
<div ng-app="myApp">
    <div ng-form="myForm">
        <your-input form="myForm"></your-input>
    </div>
</div>

</body>


</html>

The code above is rendered as follows: