var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
//the following will declare a new directive that
// may be used like
app.directive('myDirective', function(){
// The above name 'myDirective' will be parsed out as 'my-directive'
// for in-markup uses.
return {
// restrict to an element (A = attribute, C = class, M = comment)
// or any combination like 'EACM' or 'EC'
restrict: 'E',
scope: {
name: '=name' // set the name on the directive's scope
// to the name attribute on the directive element.
},
//the template for the directive.
template: '<div>Hello, {{name}} <button ng-click="reverseName()">Reverse</button></div>',
//the controller for the directive
controller: function($scope) {
$scope.reverseName = function(){
$scope.name = $scope.name.split('').reverse().join('');
};
},
replace: true, //replace the directive element with the output of the template.
//the link method does the work of setting the directive
// up, things like bindings, jquery calls, etc are done in here
link: function(scope, elem, attr) {
// scope is the directive's scope,
// elem is a jquery lite (or jquery full) object for the directive root element.
// attr is a dictionary of attributes on the directive element.
elem.bind('dblclick', function() {
scope.name += '!';
scope.$apply();
});
}
};
});
<!doctype html>
<html ng-app="plunker" >
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css">
<script>document.write("<base href=\"" + document.location + "\" />");</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<my-directive name="name"></my-directive>
</body>
</html>
/* Put your css in here */