【发布时间】:2017-04-12 07:09:42
【问题描述】:
问题来了,我需要创建一个动态“导入”另一个指令的指令。为此,我将这个其他指令的属性添加到元素并重新编译。
我在 fiddle 上添加了一个示例,用于处理每个可能的事件(来自元素或子元素,来自 Angular 或 jQuery)。我尽我所能,删除并重新附加子项,删除并读取 html,但无论我尝试什么,我总是至少重复一个事件或一个事件消失。
http://jsfiddle.net/Lvc0u55v/12673/
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div ng-controller="testCtrl">
<div id="container-test" new-tooltip="New Tooltip" ng-click="clickContainer()">
<button id="btn-test" ng-click="clickBtn()">Testing</button>
</div>
</div>
Javascript
var myApp = angular.module('myApp',[]);
function testCtrl($scope) {
jQuery('#btn-test').click(function(){
alert('jquery button');
})
$scope.clickBtn = function() {
alert('ng-click button');
}
jQuery('#container-test').click(function(){
alert('jquery container');
})
$scope.clickContainer = function() {
alert('ng-click container');
}
}
myApp.directive('newTooltip', ['$timeout', '$compile',
function($timeout, $compile) {
return {
restrict: 'A',
scope: false,
link: function(scope, element, attrs) {
var value = attrs['newTooltip'];
if (element.attr('uib-tooltip') != value) {
element.attr('uib-tooltip', value);
$compile(element)(scope);
}
}
};
}
]);
myApp.directive('uibTooltip', ['$timeout', '$compile',
function($timeout, $compile) {
return {
restrict: 'A',
scope: false,
link: function(scope, element, attrs) {
element.attr('title', attrs['uibTooltip']);
}
};
}
]);
您对如何解决此问题有任何想法吗?
【问题讨论】:
标签: jquery angularjs angularjs-directive jquery-events