How can I use a media query in ng-style - angularjs

I would like to set the width of an element using ng-style and a media query.
I have tried this:
{'width' : #media (max-width: 480px) ? '70%' : '90%' , 'border-radius': '6px', 'background':'#F5F2ED'}
But it doesn't seem to work.
Any ideas?

There is a trick to this problem,
create another inner <div> in your outer <div>
It works because you can reverse the effect of your applied style, by setting the values as 0, none or initial.
Example:
The effects you want for your<div>
IN TS: setting background-color according to the data parsed in from parent. (unable to do in CSS)
IN CSS: no background when the screen width is larger than 1024px (you do not want to check width to change accordingly in TS)
The problem you faced is that:
When using [ngStyle] to set the style, it will overwrite your media query for devices larger than 1024 px.
The solution is that: you create a inner div and set the css as:
outer div | color according to the data parsed in, use [ngStyle]
max-width | more than 1024 | less than 1024
inner div | background-color : white | background-color : initial
In your HTML
<div [ngStyle]="'background-color':color[2]">
<div class="inner">
some component
</div>
</div>
In your CSS
.inner{
background-color: white;
}
#media (max-width:1024px){
background-color: initial;
}
In your TS
#Input() color
This will reverse the effect or set it to none so that outer div color can be applied.
Also, note that the CSS for inner div for more than 1024px should be your default website theme background, so change to anything which is your default background.
Because they are in different div outer div cannot overwrite inner div.

Let's see to this directive source:
var ngStyleDirective = ngDirective(function(scope, element, attr) {
scope.$watch(attr.ngStyle, function ngStyleWatchAction(newStyles, oldStyles) {
if (oldStyles && (newStyles !== oldStyles)) {
forEach(oldStyles, function(val, style) { element.css(style, '');});
}
if (newStyles) element.css(newStyles);
}, true);
});
There is nothing to support this functionality.

Related

How to check an element is having display block / none style in Protractor?

I'm having two div's which are having same css, only difference is the style is having display:block and none.
<div class="autocomplete-suggestions " style="left: 91px; top: 333px; min-width: 747px; display: none;">
<div>item1</div>
<div>item2</div>
</div>
<div class="autocomplete-suggestions " style="left: 91px; top: 333px; min-width: 747px; display: block;">
<div>item3</div>
<div>item4</div>
</div>
How we could identify the which element is having the style display block or none in Protractor?
I need to click item3 div which is reside with display:block div.
I have tried the below code.
browser.findElements(by.css('.autocomplete-suggestions')).then((autoSuggestions) => {
autoSuggestions.map((item) => {
if (item.isDisplayed()) {
item.getTagName().then((x) => {
console.log('tagname', x);
});
browser.pause();
//item[index].click();
}
})
from the above code i can see both div's.
I'm receiving 'could not find element 'click on undefined' error.
I'm trying end to end testing with Anuglar 7, protractor, jasmine and selenium web driver.
You need to filter your expression to take into consideration the style attribute.
You might find XPath selector easier to write/read/understand, you can use XPath contains() function to select only div which has display: block in the style attribute would be:
//div[contains(#class,'autocomplete-suggestions') and contains(#style, 'display: block')]/div[1]
Demo:
If you would like to stay with CSS selectors the equivalent expression will be:
div[class*="autocomplete-suggestions"][style *= "display: block"] >:nth-child(1)
If you're looking for an option using CSS as locator then you can use this
element(by.cssContainingText(".autocomplete-suggestions[style*='display: block'] div", "item3"))

PrimeNG Tooltip Inner Will not change color

When modifying the ui-tooltip css it only changes the outer color of the tooltip, it still leaves an inner color. I also tried the CSS from jQuery UI which should make a black tooltip but again the inner color did not change.
Does anyone have any experience with this?
https://www.primefaces.org/primeng/#/tooltip
<input type="text" pTooltip="Enter your username" tooltipPosition="top">
CSS:
.ui-tooltip {
background-color: red
}
Above just leaves a dark-grey block inside a red block.
This selector should work
.ui-tooltip-text.ui-shadow.ui-corner-all {
background-color: red;
}
If you are applying CSS inside your Component's CSS file than it will not apply on the tooltip because tooltip is added in body root path. So you have to add CSS in Global CSS file of your project which were located in your project src > assets > css Or you can also apply in src > assets > styles.scss.
Use below CSS:
.ui-tooltip .ui-tooltip-text {
background-color: red !important;
}

Add CSS to react created elements like data-reactroot

Under my root div react creates another div "automatically". Is there a way to add a class to that div? I need to add height: 100% to it to prevent the background content to scroll in mobile when an overlay is shown.
This is how it is shown when i inspect the element on the site. I need to add height:100% to the data-reactroot div when a button is clicked. But that div is nowhere in my source code.
<div id="root">
<div data-reactroot data-reactid="1" data-react-checksum="161698...
I could add this code in the container's componentDidMount()
let root = document.querySelectorAll('[data-reactroot]')[0];
if (root) {
root.style.height = '100%';
}
But isnt there a better way to do it? This feels like kind of hacky (in a bad way)
#root > [data-reactroot] { height: 100% } in your CSS should work.
Or just the tag itself:
[data-reactroot] { ... }
If you want to use in-line css, just make your top div position is absolute and left, top, right, bottom is zero.
So if you inspect the element:
<div id="root">
<div data-reactroot>
<div style="position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px>
....
So simple by using the id reference
#root{
/* your css here! */
}

How to set coloring of tags in ngTagsInput?

I want to use ng-tags-input in my project.
I try to set diffrent color for each tag according to color property object in array.
Here is plunker I am working on.
For this purpose I need to override tag-item css class in ng-input template.
Here is the example of ng-input template:
<script type="text/ng-template" id="tag-template">
<div class="tags-input" ng-style="{background: data.color}">
<span>{{$getDisplayText()}}</span>
<a class="remove-button" ng-click="$removeTag()">✖</a>
</div>
in this row I try to override tags-input css class:
<div class="tags-input" ng-style="{background: data.color}">
And here is result I get:
as you can see on left and right edges not colored.
Any idea what I am doing wrong?And how to override the tags-input css class??
If you look at the markup, you'll see that the .tags-input div where you apply your background color style is embedded into an li element, which has a laft and right padding of 5px. That's why the color is not applied on the full width of the button.
So, make sure to apply your own stylesheet after the ng-tags-input stylesheet, and override some CSS rules in order for the lito have no padding, and for the div with the background color to have a padding instead:
/* override default tag styles for colors to look less ugly */
tags-input .tags .tag-item {
padding: 0;
height: 27px;
}
.tags-input {
padding: 0 5px;
border-radius: 2px;
}
Here's your plunkr modified to make that happen.

Styling text color in angularjs at runtime

I have a html page with this code
<p id="roonnameDiv" >{{chatRoom}}</p>
and an app.js with the following code . It reflects the value corrrectly but if I try to style it with color at runtime it doesnt not reflect on the html page
$scope.$parent.chatRoom = $stateParams.roomId;
$scope.$parent.chatRoom.style = {"color":"green"};
I even tried using ng-color but in vain . Have head using html-unsafe tags t add html5 code to angular variables at runtime , perhaps I could use that to provide style of element but could not find any examples .
Essentially the requirement is of having various styled ( color ,size and fonts ) in roonnameDiv using angular framework
..................Edit .............................
I used the ngstyle as suggested by answers below
$scope.$parent.chatRoom = $stateParams.roomId;
$scope.myStyle = {color: "green"};
however the output text was just plain grey . On exploring it thorugh chorome inspector , I found it is inheriting some styles through body.
Switching off the body color tag just turns the text black instead of green .
Following is the body
<body ng-app="xyz" ng-controller="AppController" class="ng-scope">
This is the body style
body {
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 14px;
line-height: 1.42857143;
color: #333;
background-color: #fff;
}
I want specific style class to apply for different text components without affetcting the overall body style . Could you suggest how to do so ?
You can use ng-style directive.
In Markup
<p id="roonnameDiv" ng-style="myStyle">{{chatRoom}}</p>
In controller
$scope.myStyle = {color: "green", background: "blue"} // Write all the required styles here.
More on ng-style directive at: https://docs.angularjs.org/api/ng/directive/ngStyle
try this
<p id="roonnameDiv" ng-style="chatRoom.style" >{{chatRoom}}</p>

Resources