Angular JS Magazine

miercuri, 8 octombrie 2014

Angular Custom Directive and Transclusion Sample

In this tip, you can see an example of using Angular JS transclusion, which means to insert one part of a document into another by reference. When a custom directive wraps arbitrary content, the transclusion can be an elegant approach. Below you can see a directive that represents a popup container, and how the transclusion  is used to nest arbitrary content in it:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../angular/angular.min.js"></script>
    <link href="./css/popup.css" rel="stylesheet"/>
    <script type="text/ng-template" id="popupTemplate">
        <div id="popupTrigger">
            <div id="popupText">{{popuptitle}}</div>
            <div id="popupContainer">
                <div id="popupContent">
                    <div ng-transclude></div>
                </div>
            </div>
        </div>
    </script>
    <script>
        angular.module("appModule", [])
                .controller("appCtrl", function ($scope) {
                    $scope.players = [
                        { name: 'Nadal, Rafael (ESP)', rank: 1, age: '28', prizemoney: 66149345 },
                        { name: 'Djokovic, Novak (SRB)', rank: 2, age: '27', prizemoney: 70704129 },
                        { name: 'Federer, Roger (SUI)', rank: 3, age: '33', prizemoney: 84827704 },
                        { name: 'Wawrinka, Stan (SUI)', rank: 4, age: '29', prizemoney: 13155060 },
                        { name: 'Ferrer, David (ESP)', rank: 5, age: '32', prizemoney: 24034072 },
                        { name: 'Tsonga, Jo-Wilfried (FRA)', rank: 11, age: '29', prizemoney: 1708240 },
                        { name: 'Simon, Gilles (FRA)', rank: 26, age: '29', prizemoney: 760469 },
                        { name: 'Lopez, Feliciano (ESP)', rank: 20, age: '33', prizemoney: 1100579 },
                        { name: 'Benneteau, Julien (FRA)', rank: 28, age: '32', prizemoney: 617688 },
                        { name: 'Verdasco, Fernando (ESP)', rank: 33, age: '30', prizemoney: 689219 },
                        { name: 'Mayer, Leonardo (ARG)', rank: 25, age: '27', prizemoney: 946294 }
                    ];
                })
                .directive("popup", function () {
                    return {
                        template: function () {
                            return angular.element(
                                    document.querySelector("#popupTemplate")).html();
                        },
                        scope: {
                            popuptitle: "@titleattr"
                        },
                        restrict: 'E',
                        transclude: true
                    }
                });
    </script>
</head>
<body>
<div ng-app="appModule" ng-controller="appCtrl">

    <popup titleattr="Player Bio">
        <h4>Rafael Nadal Bio</h4>
        <p>
            Full name is Rafael Nadal Parera...Nicknamed "Rafa"...
                   Plays left-handed but writes right-handed...Began
            playing tennis at age four ...
        </p>
    </popup>
    <br/>
    <popup titleattr="Next Match">
        <h4>Next Match</h4>
        <table>
            <tr>
                <td>
                    {{players[0].name}}
                </td>
                <td>
                    {{players[5].name}}
                </td>
            </tr>
            <tr>
                <td>
                    <img src="./images/rafa.png"/>
                </td>
                <td>
                    <img src="./images/tonga.png"/>
                </td>
            </tr>
        </table>
        </p>
    </popup>
</div>
</body>
</html>