Frage

Ich bekomme allmählich den Hang von Winkelrichtlinien und hat bisher mit der Erstellung eines Dienstes als Vermittler zwischen Controller zurückgegriffen.

Ich habe mich nur gefragt, im Rahmen von Richtlinien (und Verknüpfungsfunktionen) ist es möglich, dem Controller Zugriff auf Variablen aus der Verknüpfungsfunktion zu geben?(Ohne Service oder globale Variablen).

generasacodicetagpre.

Ich möchte, dass der Wert des Sammelname-Attributs in meinem Controller verfügbar ist, damit ich entsprechende Socket-Anrufe tätigen kann.Jede Idee?

War es hilfreich?

Lösung

Sie teilen den gleichen Geltungsbereich, also sollte dies funktionieren.

generasacodicetagpre.

Andere Tipps

Sie können ein Verfahren auf dem Controller hinzufügen und den Anruf aus der Verbindungsfunktion anrufen.

generasacodicetagpre.

auf link:

generasacodicetagpre.

There are a couple of ways of doing what you want

  • Just put everything in the link function. You can set functions and variables on the scope just like you might put in a controller.

  • Just put everything in the controller, in terms of setting scope variables or functions. It is injected with $attrs, which contains the normalised attribute values, so you have access to the attributes if you need them.

As far as I know, in most cases it doesn't make a difference where you assign variables or functions on the scope. The main difference between the two is that if you want to give your directive a public API, so other directives can communicate to it via require, then you must use this.something in the controller.

There maybe a better way to do it, but I've managed to get around the problem by changing my controller functions dependencies to include an $element argument. Then just used jqLite to get the value of the attribute in question.

controller: function($scope, $element, socket) {
   var name = $element.attr('collection-name');
}

It's not fantastically elegant, but it works.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top