Search code examples
javascriptangularjsangularjs-ng-clickangular-directive

AngularJS ng-click element within directive often unclickable, or clickable outside of element


I have a simple AngularJS directive that contracts a title if the title exceeds a certain length. When contracted, there is a small chevron icon that is clickable to show the full title. When the full title is displayed, another chevron is shown to contract it. Occasionally the first chevron will only be clickable about 10 pixels above the icon itself, and often the 'contract' icon will be completely unclickable, even though it is displayed.

Directive:

(function(){
    'use strict';

    angular
        .module('main')
        .directive('headerTitle', headerTitle);

    function headerTitle() {
        return {
            restrict: 'E',
            scope: {
                brand: '=',
                generic: '=',
                suffix: '='
            },

            template: '{{ brand }} <span id="title-suffix" ng-class="{abbreviated:abbreviate}">{{ generic }} {{ suffix }}</span>' +
                    '<span ng-click="showFullTitle()" ng-show="abbreviate" class="expand-title"><i class="fa fa-chevron-right"></i></span>' +
                    '<span ng-click="contractTitle()" ng-show="contract" class="contract"><i class="fa fa-chevron-left"></i></span>',

            link: function(scope, elem, attrs, fn) {
                scope.generic = (scope.generic) ? '(' + scope.generic + ')' : '';
                scope.suffix = (scope.suffix) ? scope.suffix : '';
                scope.abbreviate = ((scope.brand.length + scope.generic.length + scope.suffix.length) > 35) ? true : false;

                scope.showFullTitle = function() {
                    scope.abbreviate = !scope.abbreviate;
                    scope.contract = true;
                }

                scope.contractTitle = function() {
                    scope.abbreviate = !scope.abbreviate;
                    scope.contract = false;
                }
            }
        };
    }


})();

HTML Partial:

<div id="drugs-drugdetail" class="container">
    <div id="fullscreenonly" fullscreen="isFullScreen">
        <div class="panel panel-default">
            <div class="panel-heading report">
                <div class="h3 report-header">
                    <div id="drugdetail-report-header" class="title pull-left">
                        <header-title brand="Drug.aedrug_name" generic="Drug.aedrug_generic" suffix="TitleSuffix"></header-title>
                        <div class='drug-report-header-container'>
                            <div class='drug-report-data'>
                                <div class='primary-suspect-cases'>
                                <span class="header-label h4" id="drugdetail-cases-label">Primary Suspect Cases:</span>
                                <span class="h4" id="drugdetail-cases-value">{{ Metrics.counts.primary | number }}</span>
                                </div>
                                <div class='data-complete-through'>
                                <div class='drug-report-data'>
                                    <span class="header-label h4" id="drugdetail-date-label">Data Complete Through: <span class="h4" id="drugdetail-date-value"> {{ data_updated }}</span></span>
                                    </div>
                                </div>
                            </div>
                            <div class="drug-header-buttons">
                                <div class="header-button-padding">
                                    <button ng-click="goFullScreenViaWatcher()" class="btn btn-primary"><i class="fa aexicon-desktop"></i></button>
                                    <button ng-click="localStorageClearAll()" class="btn btn-primary reset-grid-button">Reset Grids</button>
                                    <span class="dropdown">
                                        <compare-menu categories="overviewData"></compare-menu>
                                    </span>
                       //redacted code for readability...

Solution

  • I discovered that some of the legacy code had modified line-heights which were then overlapping the chevron, making it visible but unclickable. I rewrote the html and css for the header, and now it works fine.