Angular JS Magazine

vineri, 29 august 2014

Handle Events Sample

The below code shows how to deal with events in Angular. In this case, you can see mouseover event:

<!DOCTYPE html>
<head lang="en">
    <meta charset="UTF-8">
    <script src="../angular/angular.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet" />
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet" />
        angular.module("atpModule", [])
                .controller("atpCtrl", function ($scope) {
                    $scope.handleEvent = function (e) {
                        $scope.color = e.type == "mouseover" ? "my_mouseenter" : "my_mouseleave";
        .my_mouseenter { background-color: red}
        .my_mouseleave { background-color: green}
<div ng-app="atpModule" ng-controller="atpCtrl" ng-init="atp=[
     { name: 'Nadal, Rafael (ESP)', rank: 1 },
     { name: 'Djokovic, Novak (SRB)', rank: 2 },
     { name: 'Federer, Roger (SUI)', rank: 3 },
     { name: 'Wawrinka, Stan (SUI)', rank: 4 },
     { name: 'Ferrer, David (ESP)', rank: 5 }]">

    <div id="atpPanel" class="panel">
        <h3 class="panel-header">ATP SINGLES RANKINGS</h3>
        <table class="table">
            <tr ng-repeat="item in atp" style="background-color: green;">
                <td ng-class="color"  ng-mouseenter="handleEvent($event)"

Mouse enter:

Mouse leave: