简体   繁体   中英

Angularjs directive for definite <div>

I just started learning angularjs. I find script for change font size but that script change all tags <p> on page. How change <p> only in <div class="items-list"> ?

myApp.directive('textSizeSlider', ['$document', function ($document) {
return {
    restrict: 'E',
    template: '<div class="text-size-slider"><span>Увеличение шрифта</span><input type="range" min="{{ min }}" max="{{ max }}" step="{{ step || 0 }}" ng-model="textSize" class="slider" value="{{ value }}" /></div>',
    scope: {
        min: '@',
        max: '@',
        unit: '@',
        value: '@',
        step: '@'
    },
    link: function (scope, element, attr) {
        scope.textSize = scope.value;

        scope.$watch('textSize', function (size) {
            $document[0].body.style.fontSize = size + scope.unit;
        });
    }
}

}]);

  <text-size-slider min="12" max="24" unit="px" value="18" step="0">

In your watch function you can get the element of that class name and in that you can apply changes. Like this

 scope.$watch('textSize', function (size) {
      var elementResult = element[0].getElementsByClassName('items-list');
            elementResult.style.fontSize = size + scope.unit;
   });

Although you'd be better off just using CSS for this, here is how to limit it to the active scope:

scope.$watch(function() {
  return scope.textSize;
}, function(newTextSize, oldTextSize) {

  var paragraphs = element[0].querySelector('.items-list').getElementsByTagName('p');

  var length = paragraphs.length,
      p, i;

  for (i = 0; i < length; i++) {

    p = paragraphs[i];

    p.style.fontSize = size + scope.unit;
  };
});

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