Angular JS Magazine

duminică, 28 septembrie 2014

Custom Directive - Update Ordered List

In this tip we continue the story from Custom Directive - Order List And Property Expressions. This time, we want to add a button that simulates the prize money update. In this case, the directive should be aware of data changes, so it needs a watcher, like $watch:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../angular/angular.min.js"></script>
    <style type="text/css">
    </style>
    <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("orderedList", function () {
                    return function (scope, element, attrs) {
                        var data = scope[attrs["orderedList"]];
                        var marker = attrs["markerType"];
                        var property_exp = attrs["propertyName"];
                        if (angular.isArray(data)) {
                            var listElem = angular.element("<ol type=" + marker + ">");
                            element.append(listElem);
                            for (var i = 0; i < data.length; i++) {
                                (function () {
                                    var pos = i;
                                    var itemElem = angular.element('<li>');
                                    listElem.append(itemElem);
                                    var wElem = function (wScope) {
                                        return wScope.$eval(property_exp, data[pos]);
                                    }
                                    scope.$watch(wElem, function (newValue, oldValue) {
                                        itemElem.text(newValue);
                                    });
                                }());
                            }
                        }
                    }
                });
    </script>
</head>
<body>
<div ng-app="appModule" ng-controller="appCtrl">

    <h4>Creating a Simple Custom Directive - List Directive</h4>

    <button class="btn btn-primary" ng-click="updatePrizeMoney()">
        Updated Prizes
    </button>
    <div ordered-list="players" marker-type="i" property-name="prizemoney | currency"></div>
</div>
</body>
</html>


Niciun comentariu:

Trimiteți un comentariu