简体   繁体   中英

How to create a custom angular directive which contains multiple input controls?

I am working on my first angular project. For better or for worse, I am trying to convert almost all repeatable HTML into directives. I have a requirement to let the user select a time in HH:MM format. So I need to show two SELECT elements. Since I need to give this control at quite a few places, I am trying to convert into a directive.

Directive Template

<div class="filterLabel">{{fieldLabel}}</div>
<select class="FilterDDL" ng-style="{width: selectHhWidthPx + 'px'}">
    <option value="none">HH</option>    
    <option value="8">08</option>       
    <option value="9">09</option>       
    <option value="10">10</option>      
    <option value="11">11</option>      
    <option value="12">12</option>      
    <option value="13">13</option>      
    <option value="14">14</option>      
    <option value="15">15</option>      
    <option value="16">16</option>      
    <option value="17">17</option>      
</select>
<span>:</span>
<select class="FilterDDL" ng-style="{width: selectHhWidthPx + 'px'}">
    <option value="none">MM</option>    
    <option value="0">00</option>       
    <option value="30">30</option>      
</select>

My expected end result from this directive is to obtain a time value in minutes [(HH * 60 + MM)] for further calculations. However I can't think of a way in which I can get a single ngModel associated with my directive which returns the time in Minutes from the combination of two dropdowns. I read about Link function but can't figure out if I can use it in my scenario. Is it even a good practice to have custom directives span over multiple input elements?

Please refer below code:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="angular.min.js"></script>

    <script type="text/javascript">
        angular.module('demoApp', [])
            .controller('Controller', ['$scope', function ($scope) {
                $scope.totalMinutes = function () {
                    return $scope.mintutes + ($scope.hours * 60);
                }
                $scope.mintutes = 1;
                $scope.hours = 1;
            }])
            .directive('timeSelection', function () {
                return {
                    restrict: 'E',
                    template: "Hours:<input type='number' ng-model='hours' /> Minutes:<input type='number' ng-model='minutes' />",
                    scope: {
                        hours: "=hours",
                        minutes:"=mintutes"
                    }
                };
            });
    </script>
</head>
<body>
    <div ng-app="demoApp">
        <div ng-controller="Controller">
            <time-selection hours="hours" mintutes="mintutes" ></time-selection>
            Total Minutes : {{totalMinutes()}}
        </div>
    </div>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM