Multiple attributes in ng-style
ng-style waits for an object literal, so you need to adjust your code to
$scope.css = {
width: 'calc(100% -'+$scope.fixedColumnsWidth+')',
'margin-left': $scope.fixedColumnsWidth
}
<div ng-style="css"></div>
Angular ngStyle for multiple styles
When you using ngStyle
you should create a function returning one of the following: a string, an array or an object.
if you want to return an Object you do the following:
in your template:
<div [ngStyle]="styleObject()"></div>
in your component:
export class AppComponent{
styleObject(): Object {
if (/** YOUR CONDITION TO SHOW THE STYLES*/ ){
return {height: this.height,width: this.width}
}
return {}
}
}
How to apply multiple object property to ngStyle
Try this:
<span [ngStyle]="getColor(selectedOption?.type)">BLAH</span>
public getColor(type: string | undefined){
if (type) {
switch (type) {
case 'General':
return {background: '#ffe5d7', color: '#e7560a' };
case 'Interview':
return { background: '#ffe5d7', color: '#e7560a' };
//more code
case default:
return {};
}
return {};
}
EDIT: I've just read @Onur Baştürk had answerd before me.
He was right, you were duplicating the background attribute,
one in the HTML ([ngStyle]="{'background':...) and another in your ts code:
return {background: '#ffe5d7', color: '#e7560a' ....
Angular ng-style with a multiple conditional expression and multiple style attributes
You should add single quotes to 'border-color'
ng-style="{ border: ivrMessageForm.ivr_messagetitle.$error.required && !isFormValid ? '2px solid' : 'none', 'border-color': ivrMessageForm.ivr_messagetitle.$error.required && !isFormValid ? 'red' : 'none'}"
Related Topics
How to Expand a Cropped Image into a Box
How to Target Chrome Only, Not All Webkit Powered Browsers
How to Capitalize The First Letter of an Input
Import CSS Selector Styles in Another Selector? (Not @Import)
Chrome Dev Tools Showing a CSS Rule on a Grey Background. Why
CSS Table Display Differences - Chrome Vs Firefox
Select Every Second Element of a Series of Nested Elements (Each Has Only One Child)
CSS Animation Works in Chrome But Not Safari
Using Nth Child to Target Every Other Two Divs
How to Make Images in a CSS Grid Lay Next to Each Other and Jump to Another Row When Lacking Space
Compile Less to Multiple CSS Files, Based on Variable Value
Fill a Parent Div While Maintaining a Ratio
Targeting Nth Column (Made by Column-Count)
CSS Background Gradient Validation
How to Get Font to Display Properly in All Browsers