jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用jQuery绑定AngularJS元素指令上的事件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在AngularJS中有一个指令: @H_682_2@module = angular.module("demoApp",[],null); module.directive('sample',function () { return { reStrict: "E",transclude: true,replace: true,template: "<div ng-transclude></div>",controller: function ($scope,$element) { this.act = function (something) { //problematic line HERE $element.trigger("myEvent.sample",[something]); }; } }; }) .directive('item',require: "^sample",template: "<a ng-transclude style='display: inline-block; border: 1px solid crimson; margin: 2px; padding: 5px; color: crimson; text-decoration: none; BACkground: #f5d0d0; cursor: pointer;'></a>",link: function (scope,element,attributes,parentController) { element.on("click",function () { parentController.act(this.innerText); }); } } }); @H_674_4@在我的HTML中,我使用它:

<sample id="myElement">
    <item>1</item>
    <item>2</item>
</sample>
@H_674_4@哪个将呈现为:

<div ng-transclude="" id="myElement">
    <a ng-transclude="" style="display: inline-block; border: 1px solid crimson; margin: 2px; padding: 5px; color: crimson; text-decoration: none; BACkground: #f5d0d0; cursor: pointer;;display: inline-block; border: 1px solid crimson; margin: 2px; padding: 5px; color: crimson; text-decoration: none; BACkground: #f5d0d0; cursor: pointer;" class="ng-scope"><span class="ng-scope">1</span></a>
    <a ng-transclude="" style="display: inline-block; border: 1px solid crimson; margin: 2px; padding: 5px; color: crimson; text-decoration: none; BACkground: #f5d0d0; cursor: pointer;;display: inline-block; border: 1px solid crimson; margin: 2px; padding: 5px; color: crimson; text-decoration: none; BACkground: #f5d0d0; cursor: pointer;" class="ng-scope"><span class="ng-scope">2</span></a>
</div>
@H_674_4@我希望能够通过jQuery手动触发事件:

$("#myElement").on("myEvent.sample",function (e,something) {
    alert("click: " + something);
});
@H_674_4@我想在点击链接时触发此事件.

@H_674_4@如果我在sample指令上将replace属性设置为false,则它可以正常工作.我想这是因为在事件被触发的时刻,元素样本不再存在,因此它将被内部模板替换.

@H_674_4@那么,我该如何做到这一点?

@H_674_4@如下面的答案所示,这样做不会达到我的目的:

$($element).trigger("myEvent.sample",[something]);

解决方法

请在下面找到小提琴 @H_674_4@fiddle

@H_674_4@触发器是一个jquery函数,它将适用于正确的处理程序.

$(element).trigger("myEvent.sample");
@H_674_4@希望这可以帮助

大佬总结

以上是大佬教程为你收集整理的使用jQuery绑定AngularJS元素指令上的事件全部内容,希望文章能够帮你解决使用jQuery绑定AngularJS元素指令上的事件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。