我正在使用Tippy library来创建HTML工具提示。我做了两个指令来处理tippy工具提示。
.directive('settings', function() {
return {
templateUrl: 'tippy-template.html'
};
})
.directive('tippy', function() {
return function (scope) {
tippy('.tippy', {
position: 'bottom',
animation: 'shift',
arrow: true,
interactive: true,
arrowSize: 'big',
distance: 20,
html: document.getElementById('setting-template'),
appendTo: document.getElementById('settings-controller')
})
};
})
settings
指令包含HTML工具提示代码,tippy
指令将放在HTML工具提示代码中以激活它。
tippy工具与它所在的控制器共享数据,在这个例子中它共享缓存。
如果tippy
Fiddler 1 controller只有一个实例,那么一切正常。我无法再次使用该指令。我能够重新创建我遇到的问题Fiddler 2 controllers link。
根据我的理解,Tippy
只能在有唯一ID的情况下使用。有办法解决这个问题吗?
毛尖-template.html
<div id="setting-template" tippy>
<ul class="collection">
<li class="collection-item">
<div class="col-title"><b>{{title}}</b></div>
<div class="col-title">Cache</div>
<div class="col-item">
<div class="switch">
<label>Off
<input ng-model="cache" type="checkbox"><span class="lever"></span> On
</label>
</div>
</div>
</li>
<li class="collection-item">
<div class="col-title"><b>Cache Result</b></div>
<div class="col-item">{{cache}}</div>
</li>
</ul>
</div>
指令用法(在控制器内部)
<div id="settings-controller" settings></div>
答案 0 :(得分:1)
.tippy
),我基本上直接使用了这些元素,你说它需要document.getElementById()
才能工作,但是$element[0]
做了一样。通过在控制台中记录两个输出来计算出来。无论如何,请检查下面的解决方案。
<强> HTML:强>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<body ng-app="myapp">
<div class="row">
<!-- CONTROLLER 2-->
<div class="col s12 m6" ng-controller="controller1">
<div class="card fill1">
<div class="card-content">
<span class="card-title">{{title}}</span>
<i class="material-icons tippy c-pointer" data-theme="light" data-trigger="click" data-interactive="true" data-animatefill="false" data-arrow="true">settings</i>
<div id="settings-controller2" parent="tippy" settings></div>
<div class="filler"></div>
Cache is : <b>{{cache}}</b>
</div>
</div>
</div>
<!-- CONTROLLER 2-->
<div class="col s12 m6" ng-controller="controller2">
<div class="card">
<div class="card-content">
<span class="card-title">{{title}}</span>
<i class="material-icons tippy2 c-pointer" data-theme="light" data-trigger="click" data-interactive="true" data-animatefill="false" data-arrow="true">settings</i>
<div id="settings-controller" parent="tippy2" settings></div>
<div class="filler"></div>
Cache is : <b>{{cache}}</b>
</div>
</div>
</div>
</div>
<script type="text/ng-template" id="tippy-template.html">
<div id="setting-template" tippy>
<ul class="collection">
<li class="collection-item">
<div class="col-title"><b>{{title}}</b></div>
<div class="col-title">Cache</div>
<div class="col-item">
<div class="switch">
<label>Off
<input ng-model="cache" type="checkbox"><span class="lever"></span>
On</label>
</div>
</div>
</li>
<li class="collection-item">
<div class="col-title"><b>Cache Result</b></div>
<div class="col-item">{{cache}}</div>
</li>
</ul>
</div>
</script>
</body>
<强> JS:强>
var myapp = angular.module('myapp', [])
.directive('settings', function() {
return {
templateUrl: 'tippy-template.html',
controller: function($scope, $element, $attrs){
$scope.parent = $element;
$scope.tippyClass = $attrs.parent;
}
};
})
.directive('tippy', function() {
return {
controller: function ($attrs, $scope, $element) {
console.log($attrs.id);
tippy('.'+$scope.tippyClass, {
position: 'bottom',
animation: 'shift',
arrow: true,
interactive: true,
arrowSize: 'big',
distance: 20,
html: $element[0],
appendTo: $scope.parent[0]
})
}
};
})
.controller('controller2', function($scope) {
$scope.title = "Controller 2";
$scope.cache = true;
})
.controller('controller1', function($scope) {
$scope.title = "Controller 1";
$scope.cache = false;
});
我建议您尝试使该指令更通用且易于使用,并且可能发布Github GIST,以便其他人将来可以轻松使用Tippy
!!!!