I'm working on building a dashboard in AngularJS, and I have created a "widget" directive. The problem is that the widget can be one of multiple types of widgets, and I'm varying that based on a certain property on the directive. This is really easy for templates, where I can just get the attribute from the attributes object in the link function, fetch a template, and compile it.
However, for the controller, it seems to be more difficult (or I'm just missing it). What I have so far is this:
return ['$compile', '$http', function($compile, $http) {
var templateContent = '';
return {
restrict: 'A',
scope: {},
link: function($scope, element, attributes, controller) {
element.append($compile(templateContent)($scope));
...
}
};
}];
I'm looking for a way to set the controller dynamically based on an attribute on the element. Is this possible? I don't want to explicitly set data-ng-controller on the element because I'm trying to get the HTML to only include the TYPE of widget it wants to load (not having to worry about the template location or controller for it).
Ideally, it'd look something like this:
<div data-widget="typename">...</div>