Angular JS Magazine

duminică, 2 noiembrie 2014

Angular JS - Using Controllers in Directives

In this tip you can see how to create a controller in a directive, and, after that, another directive that uses this controller:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../angular/angular.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet" />
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet" />
    <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 }
                    ];
                    $scope.updatePrizeMoney = function () {
                        angular.forEach($scope.players, function (player) {
                            player.prizemoney = player.prizemoney +
                                 Math.floor((Math.random() * (900000 - 10000) + 10000));
                        });
                    }
                })
                .directive("totalPrizemoney", function () {
                    return {
                        template: "<span class='label label-primary'>{{item.name}} -
                              {{item.prizemoney | currency}}</span>",
                        restrict: 'A',
                        require: "^orderedList",
                        link: function (scope, element, attrs, ctrl) {
                            scope.$watch("item.prizemoney", function () {
                                ctrl.updateTotalPrizes();
                            });
                        }
                    }
                })
                .directive("orderedList", function () {
                    return {
                        transclude: true,
                        scope: {playerslocal: "=playersattr", totallocal: "=totalattr"},
                        restrict: 'E',
                        template: "<ol type='i' ng-transclude></ol>",
                        controller: function ($scope, $element, $attrs) {
                            this.updateTotalPrizes = function() {
                                var total = 0;
                                for (var i = 0; i < $scope.playerslocal.length; i++) {
                                    total += Number($scope.playerslocal[i].prizemoney);
                                }
                                $scope.totallocal = total;
                            }
                        }
                    }
                });
    </script>
</head>
<body>
<div ng-app="appModule" ng-controller="appCtrl">

    <h6>Using Controllers in Directives</h6>

    <button class="btn btn-primary" ng-click="updatePrizeMoney()">
        Updated Prizes
    </button>
    <div>
        <ordered-list playersattr="players" totalattr="totalmoney">
         <li ng-repeat="item in players" total-prizemoney></li>
         <span class='label label-primary'> Total: {{totalmoney | currency}}}</span>
        </ordered-list>
    </div>
</div>
</body>
</html>

Niciun comentariu:

Trimiteți un comentariu