Hi Sander,
thank you for your reply!
I'm using the $compile function now, but I think in another way you thought
of - but it works for me :)
For each widget which is defined I now define an own directive. The dynamic
including of the directive is done with the $compile method:
var widgets=={
news: {
widget: 'news',
name: 'Heise.de Newsfeed',
directiveSrc: require('./news/news-directive'),
params: {
feed: 'heise'
}
},
links: {
widget: 'links',
name: 'Team-Links',
directiveSrc: require('./links/links-directive')
}
};
var module=[...]
.directive('dashboardWidget', function($compile) { // widget container
directive
[...]
.controller(function($scope, $element) {
for (x in widgets) {
var widget=widgets[x];
var el=$compile('<dashboard-widget-' + widget.widget + '
widget-params="widgetParams"></dashboard-widget-' + widget.widget +
'>')($scope);
var divs=$element.find('div');
for (var i=0; i<divs.length; i++) {
var div=angular.element(divs[i]);
if (div.hasClass('widget-content')) {
div.append(el);
}
}
}
}
}
});
// dynamically define the widgets as directives
for (var x in widgets) {
if (typeof(widgetsDefined[widgets[x].widget]) == 'undefined') {
var widgetUcFirst=widgets[x].widget.substring(0,1).toUpperCase() +
widgets[x].widget.substring(1);
module.directive('dashboardWidget' + widgetUcFirst,
widgets[x].directiveSrc);
widgetsDefined[widgets[x].widget]=1;
}
}
So I can easily define new widgets, which each having separate controller
and views and even scopes. Each widget can receive parameters (here:
'feed').
Maybe this helps someone, and hopefully I'm still doing the angular way...
:)
Am Mittwoch, 11. Februar 2015 16:59:05 UTC+1 schrieb Sander Elias:
>
> Hi Jan,
>
> This is something that you can do with the compile fase. the compiling is
> done before the controller is initiated, so you can let your compile
> function put the right controller in place.
> As a side note, have a look at templatecache
> <https://docs.angularjs.org/api/ng/service/$templateCache>. that will do
> the fetching of the templates also, but not over and over again, when the
> same template is reused.
>
> Is this enough for you? If not don't hesitate to ask,
> Regards
> Sander
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.