我有范围滑块的创建指令,现在它改变了主体字体,但我想为特定元素工作,你能给我代码吗
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 :(得分:0)
在指令中添加目标以使其动态化。
将 id =&#34; *&#34; 添加到您的目标,以使用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();
});
}
}
如果您有多个元素要更改尺寸或其他样式,请使用此
更改指令目标属性:
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();
});