سؤال

أنا تدريجيا الحصول على تعليق التوجيه الزاوي وحتى الآن، لجأنا إلى إنشاء خدمة كوسيط بين وحدات التحكم.

كنت أتساءل فقط، في سياق التوجيهات (وربط الوظائف) هل من الممكن إعطاء الوصول إلى وحدة التحكم إلى المتغيرات من وظيفة الارتباط؟(بدون خدمة أو متغيرات عالمية).

giveacodicetagpre.

أريد أن تكون قيمة سمة اسم المجموعة لتكون متوفرة داخل جهاز التحكم الخاص بي، حتى أتمكن من إجراء مكالمات مأخذ توصيل مناسبة.أي فكرة؟

هل كانت مفيدة؟

المحلول

يشاركون نفس النطاق، لذلك يجب أن يعمل هذا.

giveacodicetagpre.

نصائح أخرى

يمكنك إضافة طريقة على وحدة تحكم والاتصال بها من وظيفة الرابط.

giveacodicetagpre.

على الرابط:

giveacodicetagpre.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top