I have created angular responsiveButton directive, which adjusts the width of the button:
var app = angular.module('myApp', []);
app.directive('responsiveButton', function ($window) {
return function (scope, element, attrs) {
var handleMediaMatch = function (mql) {
var smallButtonWidth = 170;
var standartButtonWidth = 250;
if (mql.matches) {
element.removeClass('btn-xs');
element.addClass('btn-block');
element.removeAttr('style');
} else {
element.removeClass('btn-block');
if (attrs.buttonSize == 'small') {
element.addClass('btn-xs');
element.css('min-width', smallButtonWidth + 'px');
} else {
element.css('min-width', standartButtonWidth + 'px');
}
}
};
var mobileMaxWidth = 640;
var mql = $window.matchMedia('(max-width: ' + mobileMaxWidth + 'px)');
mql.addListener(handleMediaMatch);
handleMediaMatch(mql);
}
});
And html:
<button type="button" class="btn btn-info btn-xs" responsive-button button-size="small">Show Opening Hours</button>
It works fine here.
But I want to use it inside another openingHours directive:
var app = angular.module('myApp', []);
app.directive('openingHours', function () {
return {
restrict: 'E',
template: '<button type="button" class="btn btn-info btn-xs" responsive-button button-size="{{ buttonSize }}">Show Opening Hours</button>',
link: function (scope, element, attrs) {
scope.buttonSize = attrs.buttonSize;
}
};
});
And html:
<opening-hours button-size="small"></opening-hours>
And in this example it doesn't work as expected. If the media width is more than 640px, the button width has to be 170px, but not 250 px as in this example. And only when you make screen width smaller than 640px and then make it larger than 640px the button width became 170px as expected.
Please, help to fix this problem.