0

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?

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

I thought of this but then how would this pan out when there are multiple directives of this kind in a single ngApp. Wouldn't the models conflict with each other if we calculate them all in the global controller (controller outside the directive)?
I essentially used your approach to get what I wanted. Did a small change though. Instead of passing a simple ngModel to the input controls, I passed two object properties like StartTime.Hours and StartTime.Minutes. So I can now use different objects with my input controllers and the models don't conflict. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.