For example, UI Bootstrap has a directive called 'typeahead' that offers values ββfor a field. Let's say I want to make a directive that I can use as an attribute for an element that will call colors for this element.
Here's an attempt that fails ...
Directive
angular.module('myApp') .directive('suggestcolors', function () { return { compile: function compile(element, attrs) { attrs.$set("typeahead", "color for color in ['red', 'blue', 'green']"); } }; });
View:
<textarea ng-model="foo" suggestcolors></textarea>
When I check the text box, the attribute has been set, but it does nothing. The same thing happens if I translate the change from attrs to a communication function. Setting the typeahead attribute directly in the view works as expected:
<textarea ng-model="foo" typeahead="color for color in ['red', 'blue', 'green']"></textarea>
(But I want to be able to insert this attribute dynamically for DRY reasons. My actual use case is more complicated.)
A similar question was asked here (about dynamically adding ng-click behavior at compile time), but he never answered directly.
source share