2017-05-31 61 views
0

我創建範圍滑塊指令,現在它改變身體字體,但我要爲特定元素的工作,你可以給我的代碼爲文字大小滑塊與AngularJS特定元素

angular.module('textSizeSlider', []) 
     .directive('textSizeSlider', ['$document', function ($document) { 
      return { 
       restrict: 'E', 
       template: '<div class="text-size-slider"><span class="small-letter" ng-style="{ fontSize: min + unit }">A</span> <input type="range" min="{{ min }}" max="{{ max }}" step="{{ step || 0 }}" ng-model="textSize" class="slider" value="{{ value }}" /> <span class="big-letter" ng-style="{ fontSize: max + unit }">A</span></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; 
       }); 
       } 
      } 
      }]); 
+0

你想處理依賴於父級的子字體大小嗎? – Maher

+0

@Maher檢查這個環節,我想只改變航向字體大小 https://plnkr.co/edit/9bYR1aprS3Xn7YWyB6kj?p=preview –

+0

任何人都可以我給我解答??? –

回答

0

在您的指令中添加目標以使其具有動態性。

id =「*」添加到您的目標,以便使用JavaScript DOM檢測它。

的Html

<header ng-app="textSizeSlider"> 
    <!-- Custom AngularJS Directive --> 
    <text-size-slider min="12" max="24" unit="px" value="18" step="0" target="myHeaderTitle"> 
     <!-- END of Custom AngularJS Directive --> 
    </text-size-slider> 
</header> 

<section> 
    <h1 id="myHeaderTitle">Text Size Slider</h1> 
    <h3>AngularJS, HTML5 and CSS3</h3> 
    <article> 
     <p> 
      <strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 
     </p> 
     <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> 
    </article> 
</section> 

改變指令

return { 
controller: ctrl, 
restrict: 'E', 
template: '<div class="text-size-slider"><span class="pointer" style="left:{{position}}px;"><span>{{textSize}}</span></span><span class="small-letter" ng-style="{ fontSize: min + unit }">A</span> <input type="range" min="{{ min }}" max="{{ max }}" step="{{ step || 0 }}" ng-model="textSize" class="slider" value="{{ value }}" ng-change="updatepointer()" /> <span class="big-letter" ng-style="{ fontSize: max + unit }">A</span></div>', 
scope: { 
    min: '@', 
    max: '@', 
    unit: '@', 
    value: '@', 
    step: '@', 
    target: '@' 
}, 
link: function (scope, element, attr) { 
    scope.textSize = scope.value; 

    scope.$watch('textSize', function (size) { 
     document.getElementById(scope.target).style.fontSize = size + scope.unit; 
     scope.updatepointer(); 
    }); 
} 
} 

如果你有多個元素改變大小或其他樣式使用THI通過這種S

更改指令target屬性:

target="myHeaderTitle,myHeaderTitle2" 

定義目標

<h1 id="myHeaderTitle">Text Size Slider</h1> 
<h3 id="myHeaderTitle2">AngularJS, HTML5 and CSS3</h3> 

更改指令通過這個代碼:

var targets = scope.target.split(","); 

scope.$watch('textSize', function (size) { 
    for (key in targets) { 
     var target = targets[key]; 
     document.getElementById(target).style.fontSize = size + scope.unit; 
    } 
    scope.updatepointer(); 
}); 
+0

我有添加代碼,但還沒有成型,請codepen https://codepen.io/tarunmishra592/pen/PmMbxx –

+0

目標是標識不類 – Maher

+0

多不工作元素檢查Codepen鏈接下面也給控制檯錯誤 https://codepen.io/tarunmishra592/pen/PmMbxx –