One Method you can try is declaring and assigning the styles in JavaScript.
Declare a Style object.
styles = {
background: 'green',
color: 'black'
};
Assign the object to a template using ng-style
<div ng-style="$ctrl.styles">
My Component with local CSS
</div>
Here are some the following advantages using this method
- Using Variables to drive themes.
- Mixins through merging two Style Objects together (lodashes _.defaults)
- Control over style overwriting.
- Applying styles through logical conditions.
- Having local Styles that can't effect other components.
- You can place styles in angular services and inject them into only the component that need them.
Full Example
//Component
class myComponent {
constructor( myCSSService ) {
this.styles = {
background: 'black',
color: 'white'
};
this.myCSSService = myCSSService;
}
}
myComponent.$inject = [ 'myCSSService' ];
angular.module( 'myModule', [] )
.component( 'myComponent', {
controller: myComponent,
bindings: {},
template: `
<div ng-style="$ctrl.styles">
My Component with local CSS
</div>
<div ng-style="$ctrl.myCSSService.styles">
My Component with Injected CSS
</div>
`,
} );
//Style Service
class myCSSService{
constructor( ) {
this.styles = {
background: 'green',
color: 'black'
};
}
}
angular.module( 'myCSSModule', [] )
.service( 'myCSSService', myCSSService );
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…