Here's a solution that moves the directives that need to be added dynamically, into the view and also adds some optional (basic) conditional-logic. This keeps the directive clean with no hard-coded logic.
The directive takes an array of objects, each object contains the name of the directive to be added and the value to pass to it (if any).
I was struggling to think of a use-case for a directive like this until I thought that it might be useful to add some conditional logic that only adds a directive based on some condition (though the answer below is still contrived). I added an optional if
property that should contain a bool value, expression or function (e.g. defined in your controller) that determines if the directive should be added or not.
I'm also using attrs.$attr.dynamicDirectives
to get the exact attribute declaration used to add the directive (e.g. data-dynamic-directive
, dynamic-directive
) without hard-coding string values to check for.
angular.module('plunker', ['ui.bootstrap'])_x000D_
.controller('DatepickerDemoCtrl', ['$scope',_x000D_
function($scope) {_x000D_
$scope.dt = function() {_x000D_
return new Date();_x000D_
};_x000D_
$scope.selects = [1, 2, 3, 4];_x000D_
$scope.el = 2;_x000D_
_x000D_
// For use with our dynamic-directive_x000D_
$scope.selectIsRequired = true;_x000D_
$scope.addTooltip = function() {_x000D_
return true;_x000D_
};_x000D_
}_x000D_
])_x000D_
.directive('dynamicDirectives', ['$compile',_x000D_
function($compile) {_x000D_
_x000D_
var addDirectiveToElement = function(scope, element, dir) {_x000D_
var propName;_x000D_
if (dir.if) {_x000D_
propName = Object.keys(dir)[1];_x000D_
var addDirective = scope.$eval(dir.if);_x000D_
if (addDirective) {_x000D_
element.attr(propName, dir[propName]);_x000D_
}_x000D_
} else { // No condition, just add directive_x000D_
propName = Object.keys(dir)[0];_x000D_
element.attr(propName, dir[propName]);_x000D_
}_x000D_
};_x000D_
_x000D_
var linker = function(scope, element, attrs) {_x000D_
var directives = scope.$eval(attrs.dynamicDirectives);_x000D_
_x000D_
if (!directives || !angular.isArray(directives)) {_x000D_
return $compile(element)(scope);_x000D_
}_x000D_
_x000D_
// Add all directives in the array_x000D_
angular.forEach(directives, function(dir){_x000D_
addDirectiveToElement(scope, element, dir);_x000D_
});_x000D_
_x000D_
// Remove attribute used to add this directive_x000D_
element.removeAttr(attrs.$attr.dynamicDirectives);_x000D_
// Compile element to run other directives_x000D_
$compile(element)(scope);_x000D_
};_x000D_
_x000D_
return {_x000D_
priority: 1001, // Run before other directives e.g. ng-repeat_x000D_
terminal: true, // Stop other directives running_x000D_
link: linker_x000D_
};_x000D_
}_x000D_
]);
_x000D_
<!doctype html>_x000D_
<html ng-app="plunker">_x000D_
_x000D_
<head>_x000D_
<script src="//code.angularjs.org/1.2.20/angular.js"></script>_x000D_
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script>_x000D_
<script src="example.js"></script>_x000D_
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
_x000D_
<div data-ng-controller="DatepickerDemoCtrl">_x000D_
_x000D_
<select data-ng-options="s for s in selects" data-ng-model="el" _x000D_
data-dynamic-directives="[_x000D_
{ 'if' : 'selectIsRequired', 'ng-required' : '{{selectIsRequired}}' },_x000D_
{ 'tooltip-placement' : 'bottom' },_x000D_
{ 'if' : 'addTooltip()', 'tooltip' : '{{ dt() }}' }_x000D_
]">_x000D_
<option value=""></option>_x000D_
</select>_x000D_
_x000D_
</div>_x000D_
</body>_x000D_
_x000D_
</html>
_x000D_