Angular: Directive using controller
Directives using Controller
by Pratik Bhattachary
HTML
<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<employee></employee>
JavaScript
var myApp = angular.module('myApp', []);
myApp.controller("employeeController", function($scope) {
this.addEmployee = function(emp) {
alert("Adding a new Employee - " + emp);
}
$scope.refresh = function() {
$scope.employees = $scope.employees;
}
});
myApp.directive("employee", function() {
return {
scope: {},
restrict: "A/E",
controller: "employeeController",
template: "Employee - <input type='text' ng-model='empl'><br/><button ng-click='create()'>Add</button>",
link: function(scope, ele, attr, ctrl) {
scope.create = function() {
ctrl.addEmployee(scope.empl);
}
}
}
})