I am struggling with following understanding: How can I add media (query) depending styles dynamically to elements/the DOM?
Following issues I ran into:
1) I know that AngularJS cannot manipulate tags like
<style> {{myMediaQueryStyles}} </style>
in a directive.
2) Media queries injected as inline styles with ng-style
doesn't work either
I have the following view-model.json
{
"id":"936DA01F-9ABD-4D9D-80C7-02AF85C822A8",
"contexts":[
"@media (max-width: 1200px){ … }",
"@media (max-width: 760px){ … }",
"@media (max-width: 420px)){ … }"
]
}
In a simple directive I just try to write them into a <style>
tag
app.directive('addStyles', function()
{
return {
template: '<style>{{view-model.contexts}}</style>'
};
});
Is there any workaround to add media relevant styles dynamically to elements or the whole document in angularJS?
Changing the directive template: '<div>{{view-model.contexts}}</div>'
works but the styles then just won't get applied.
Thanks in advance!
I am struggling with following understanding: How can I add media (query) depending styles dynamically to elements/the DOM?
Following issues I ran into:
1) I know that AngularJS cannot manipulate tags like
<style> {{myMediaQueryStyles}} </style>
in a directive.
2) Media queries injected as inline styles with ng-style
doesn't work either
I have the following view-model.json
{
"id":"936DA01F-9ABD-4D9D-80C7-02AF85C822A8",
"contexts":[
"@media (max-width: 1200px){ … }",
"@media (max-width: 760px){ … }",
"@media (max-width: 420px)){ … }"
]
}
In a simple directive I just try to write them into a <style>
tag
app.directive('addStyles', function()
{
return {
template: '<style>{{view-model.contexts}}</style>'
};
});
Is there any workaround to add media relevant styles dynamically to elements or the whole document in angularJS?
Changing the directive template: '<div>{{view-model.contexts}}</div>'
works but the styles then just won't get applied.
Thanks in advance!
<style>{{view-model.contexts[0]}}</style>
either.
– user2983883
Commented
Nov 12, 2013 at 15:59
{{view-model.contexts.join('')}}
– charlietfl
Commented
Nov 12, 2013 at 16:06
One workaround would be to add the relevant styles to the element in a custom directive's link
function, as shown in this Plunk.
In a simple case, in which the viewModel is made available directly in the controller, the directive would look like this:
app.directive('addStyles', function()
{
return function(scope, el, attrs) {
el.text(scope.viewModel.contexts.join('\n'));
};
});
You could also load the JSON from a separate file or remote source (as shown in the Plunk) by tweaking the directive:
app.directive('addStyles', function($http)
{
return function(scope, el, attrs) {
$http.get('viewModel.json').then(function (result) {
el.text(result.data.contexts.join('\n'));
});
};
});
This would allow for dynamically updating the styles. This is demonstrated in the Plunk by adding a $timeout to the directive that clears the styles after 3 seconds (you can see the text change from colored to black).
In either case, the relevant HTML markup would be <style add-styles></style>
.