AngularJs ng-style ::after - css

I'm trying to edit the border-bottom-color individual depending on a property of the element in ng-repeat.
Here is an example how the html is structured. The changed style is
.active-tool::after {border-bottom-color: rgb(247, 153, 248)}
html:
<div data-ng-repeat="row in rows">
<div class='container'>
<div
data-ng-style="getPrimaryColor(tvShow)"
class='folder tvshow'
data-ng-class="isActiveFolder(tvShow)"
id='{{tvShow.id}}'
data-ng-repeat="tvShow in row track by $index">
<div data-ng-click="setSelectedTvShow(tvShow)">
<p class="tvshow-name">{{tvShow.name}}</p>
</div>
</div>
</div>
controller.js
$scope.isActiveFolder = function(tvShow) {
if($scope.selectedTvShow !== null && tvShow.id !== null) {
return $scope.selectedTvShow===tvShow.id ? 'active-tool' : '';
}
};
$scope.getPrimaryColor = function(tvShow) {
if($scope.selectedTvShow !== null) {
var result = '{' + tvShow.id + '.active-tool::after {border-bottom-color: rgb(247, 153, 248)}}';
console.log(result);
return result;
};
Any ideas how this could be done?

I use this quick hack:
put this inside your template:
<style type="text/css">
.active-tool::after {
border-bottom-color: {{getShowBorderColor(tvShow)}};
}
</style>
and then in your controller:
$scope.getShowBorderColor = function(tvShow){
return tvShow.color; // change this for how you want to calculate the color
};

You cannot use ng-style like this, because html style attribute does not support css selectors.
Actually, you can do it with no javascript at all:
markup:
<div data-ng-repeat="row in rows">
<div class='container'>
<div class='folder tvshow'
data-ng-class="{active-tool : selectedTvShow === tvShow.id}"
id='{{tvShow.id}}'
data-ng-repeat="tvShow in row track by $index">
<div data-ng-click="selectedTvShow = tvShow.id">
<p class="tvshow-name">{{tvShow.name}}</p>
</div>
</div>
</div>
</div>
css:
.active-tool::after {
border-bottom-color: rgb(247, 153, 248);
}

Related

Hide parent div if two inner divs are empty

Lots of examples can be found to hide a parent div when ONE inner div is empty, but in my case I need to hide the parent div if TWO inner divs are empty:
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
Background: I am using Angular with ng-content select to fill the child divs with content. Sometimes none of these templates are used thus both child divs will be empty.
<div class="parent">
<div class="child1"><ng-content select="[child1]"></ng-content></div>
<div class="child2"><ng-content select="[child2]"></ng-content></div>
</div>
Sorry I run out of time, but would be a shame to not share what I was trying to make. Maybe not perfect because of the time but hopefully you get the idea. PS using jQuery.
$(".parent").each(function() {
var empty1 = 0;
var empty2 = 0;
var who = $(this);
$(this).find(".check").each(function() {
var check = $(this).html();
if(check == '<ng-content select="[child1]"></ng-content>') {
var empty1 = 1;
}
if(check == '<ng-content select="[child2]"></ng-content>') {
var empty2 = 1;
}
if(empty1 == 1 && empty2 == 1) {
$(who).slideUp(100);
}
});
});
.parent {
height:10vh;
background:#F00;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="parent">
<div class="check child1"><ng-content select="[child1]"></ng-content></div>
<div class="check child2"><ng-content select="[child2]"></ng-content></div>
</div>
<div class="parent">
<div class="check child1"><ng-content select="[child1]">a</ng-content></div>
<div class="check child2"><ng-content select="[child2]"></ng-content></div>
</div>
I solved it using Angular ViewChild:
<div class="column column-3" [class.hidden]="!hasContentInColumn3">
<div class="line-1" #c3a>
<ng-content select="[c3a]"></ng-content>
</div>
<div class="line-2" #c3b>
<ng-content select="[c3b]"></ng-content>
</div>
</div>
export class ListItemComponent implements OnInit {
public hasContentInColumn3 = false;
#ViewChild('c3a', { static: true }) c3a: ElementRef;
#ViewChild('c3b', { static: true }) c3b: ElementRef;
ngOnInit () {
// Hide div.column-3 when line-1 and line-2 are not provided
this.hasContentInColumn3 = (
(this.c3a.nativeElement.childNodes.length > 0) ||
(this.c3b.nativeElement.childNodes.length > 0)
);
}
}
I hope this helps someone.

mergeStyles can't child (by id) of parent (by class)

For something like the following:
<div class="example">
<div id="label">
Label text
</div>
</div>
The following CSS is able to style 'Label text'
.example #label {
color: red;
}
But it does not work with fluentUI mergeStyles:
const RedLabelStyles = mergeStyles({
".example #label": {
color: "red"
}
});
I know it's a problem with the selector, not the CSS itself, because the following correctly applies styles:
const RedLabelStyles = mergeStyles({
"#label": {
color: "red"
}
});
Was due to the RedLabelStyles selector being applied to the <div class="example"> itself. It works as expected if the JSX is as follows:
<div class={RedLabelStyles}>
<div class="example">
<div id="label">
Label text
</div>
</div>
</div>

Condition changes in ngClass but class is not updated

I'm trying to hide and show the div on click event of icon.for that I am using ngClass with condition.When variable in condition changes the class remains same ,it's not updating the class.
//html
<div class="column icon">
<mat-icon (click)="onIconClick()" id="icon" class="column" [ngStyle]="showpanel ? {'color':'#4ac2f7'}:''">filter_list</mat-icon>
</div>
//hide and show the div
<div class="search-box-container" id="filter-tab" [ngClass]="'showpanel' ? 'show' : 'hidden'">
</div>
//onclick icon .ts code
onIconClick() {
if (this.showpanel) {
this.showpanel = false;
} else {
this.showpanel = true;
}
console.log(this.showpanel);
}
Remove Condition(showpanel) single quote
Try this:
<div class="search-box-container" id="filter-tab" [ngClass]="showpanel ? 'show' : 'hidden'">
</div>
This [ngClass]="'showpanel' ? 'show' : 'hidden'" pattern is taking your showpanel as a string, so it will always evaluated as true and return 'show' class. Remove the single quote mark and it shall evaluate correctly
You have not added CSS in your question. Although, I checked your code. You only need to remove quotes from showpanel.
Please see below code -
In app.component.html
<button (click)="onIconClick()" id="icon" class="column" [ngStyle]="showpanel ? {'color':'#4ac2f7'}:''">filter_list</button>
<div class="search-box-container" id="filter-tab" [ngClass]="showpanel ? 'show' : 'hidden'">
filter-tab
</div>
In your aap.component.ts
showpanel = false;
onIconClick() {
if (this.showpanel) {
this.showpanel = false;
} else {
this.showpanel = true;
}
console.log(this.showpanel);
}
In your app.component.css
.show {
display: block;
}
.hidden {
display: none;
}
Replace 'div' with 'mat-icon'
Also, you can check the working example here - https://stackblitz.com/edit/angular-xsvxd6
<button (click)="showpanel= !showpanel" id="icon" class="column" [ngStyle]="showpanel ? {'color':'#4ac2f7'}:''">filter_list</button>
<div class="search-box-container" id="filter-tab" [ngClass]="showpanel ? 'show' : 'hidden'">
filter-tab
</div>
Css
.show {
display: block;
}
.hidden {
display: none;
}
without Function even you can do the same thing.
Stackblitz link here
and if you want function to do the same then
onIconClick() {
this.showpanel =! this.showpanel;
}

How to I change my CSS according to JSON data value?

Suppose one of my keys has value Green/Red and I want to show bootstrap btn-success when its value is green and btn-danger when its red.
You need to update ng-class
<div ng:controller="CartForm">
<ul>
<li ng-repeat='country in countries'><a class="btn" ng-class="{'btn-primary': country.color == 'green', 'btn-danger': country.color == 'red'}" href="">{{country.name}} - {{country.population}}</a></li>
</ul>
</div>
and delete appliedClass function
Working jsFiddle
I think you are looking for ng-class. You can use the following synthax:
<p ng-class="condition ? 'classIfTrue' : 'classIfFalse'">Foo</p>
In your case, it would be something like:
<p ng-class="myVariable ? 'btn-success' : 'btn-danger'">Foo</p>
As you did not provide a code sample, here is how it works with an example:
angular.module('app', []).controller('MyCtrl', function() {
this.valueSuccess = false;
this.valueDanger = true;
});
.danger { background-color: red; }
.success { background-color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<section ng-app="app">
<div ng-controller="MyCtrl as vm">
<p ng-class="vm.valueSuccess ? 'danger' : 'success'"> {{vm.valueSuccess}}</p>
<p ng-class="vm.valueDanger ? 'danger' : 'success'">{{vm.valueDanger}}</p>
</div>
</section>

CSS3: Change Page Background Image if Image is Hovered

I am trying to change the page's background image depending on the image being hovered.
This is the layout of the page:
HTML:
<div id="main">
<img id="img1" src="1.jpg" />
<img id="img2" src="2.jpg" />
</div>
CSS:
#img1:hover #main
{
background: url('images/1.jpg'); /* not working */
}
#img2:hover #main
{
background: url('images/2.jpg'); /* not working */
}
'#main' is the ID I set for the tag.
Any ideas?
If you need change the background-image of #main div you should use CSS and jQuery:
http://jsfiddle.net/Soldier/cyAXv/1/
HTML
<body>
<div id="main">
<h1>Hi!</h1>
<img id="img1" src="img1"/>
<img id="img2" src="img2"/>
</div>
</body>
JS
$('#img1').hover(function() {
$('#main').css("background","url('background1')");
})
$('#img2').hover(function() {
$('#main').css("background","url('background2')");
})
You can't traverse backwards in CSS selectors. That is to say, you can't apply a style to an ancestor/parent based on the state of a child/descendant.
You will need to use JavaScript unfortunately. You can use classes and define the styles in CSS though to make it less lame. Something like this:
jsFiddle
HTML
<div id="main">
<div id="img1"></div>
<div id="img2"></div>
</div>
CSS
#main.img1 {
background: url('https://www.google.com.au/images/srpr/logo4w.png');
}
#main.img2 {
background: url('https://www.google.com.au/images/srpr/logo4w.png');
}
#img1,
#img2 {
width:100px;
height:100px;
background-color:#F00;
margin:10px;
}
JavaScript
var main = document.getElementById('main'),
img1 = document.getElementById('img1'),
img2 = document.getElementById('img2');
img1.onmouseover = function () {
main.className = 'img1';
};
img2.onmouseover = function () {
main.className = 'img2';
};
img1.onmouseout = function () {
main.className = '';
};
img2.onmouseout = function () {
main.className = '';
};

Resources