Binden von Ereignissen auf AngularJS-element-Richtlinien mithilfe von jQuery

Habe ich eine Direktive in AngularJS:

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', function () {
    return {
        restrict: "E",
        require: "^sample",
        transclude: true,
        replace: true,
        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);
            });
        }
    }
});

Und in meine HTML ich benutze es so:

<sample id="myElement">
    <item>1</item>
    <item>2</item>
</sample>

Welche dargestellt werden als:

<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>

Will ich hören können Ereignisse ausgelöst werden, manuell durch jQuery:

$("#myElement").on("myEvent.sample", function (e, something) {
    alert("click: " + something);
});

Möchte ich dieses Ereignis ausgelöst werden, wenn der link geklickt wird.

Wenn ich die replace Eigenschaft false auf die sample Richtlinie, es funktioniert. Ich denke, es ist weil an der Stelle, wo das Ereignis ausgelöst wird, das element sample existiert nicht mehr, und als solches wird es ersetzt worden durch die inneren Vorlage.

So, wie kann ich dies erreichen?

Tun dies, wie bereits in der Antwort unten nicht erreichen, mein Ziel:

$($element).trigger("myEvent.sample", [something]);
  • link: function($scope, $element){ $element.klicken Sie auf(Funktion(){alert('clicked'):});}
InformationsquelleAutor Milad Naseri | 2014-02-21
Schreibe einen Kommentar