I'm developing a web app with AngularJS. I had an error which appeared from nowhere, it was working fine and I really don't know why it doesn't work anymore.
So it's in a directive, I try to set property offsetWidth
of an element in the directive html template. Since I really don't know where it could come from, I'll give you full code of my directive and template. The directive creates an app button and handles its visual effect on click. It's the line $element[0].offsetWidth = $element[0].offsetWidth;
which triggers the error. (It's a trick to restart animation)
I repeat again, this code was working fine and I'm almost sure I didn't make any changes. I'm using Chrome to debug, maybe it comes from it ?
Error: Uncaught TypeError: Cannot set property offsetWidth of #<HTMLElement> which has only a getter
Directive:
'use strict';
XVMApp.directive('appButton', ['$location', function($location){
return {
restrict: 'E',
replace: true,
scope: {
img: '@',
fillPercent: '@?',
target: '@?'
},
link: function ($scope, $element) {
// Image percentage fill button
if($scope.fillPercent === undefined) $scope.fillPercent = '100';
// Click event
$element.on('click', function() {
// Url target
if($scope.target !== undefined){
$scope.$apply(function() {
$location.path($scope.target);
});
}
// Click effect
$element[0].preventDefault;
$element[0].classList.remove('app-button-click-effect');
$element[0].offsetWidth = $element[0].offsetWidth;
$element[0].classList.add('app-button-click-effect');
});
},
templateUrl: 'src/directive/appButton/app-button.html'
};
}]);
Template:
<div class="app-button"
style="background-size: {{fillPercent}}%; ">
<div style="
background-image: url('src/assets/img/{{img}}');
background-repeat: no-repeat;
background-position: center;
background-size: {{fillPercent}}%;
height: 100%;
width: 100%; ">
</div>
</div>