AngularJS - Directives

directives talking to controllers

by manoj

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.13/angular.min.js"></script>
<div ng-app="myApp">
    <div ng-controller="appCtrl">
    <div enter>Roll over to load data</div>
</div>

JavaScript

var myApp = angular.module('myApp', []);

myApp.controller('appCtrl', function($scope) {
    $scope.loadData = function() {
        alert('loading data..');
    };
});

myApp.directive('enter', function () {
    return function (scope, element, attrs) {
        element.bind("mouseenter", function() {
            scope.loadData();
        });
    };
});