This repository was archived by the owner on Jan 6, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -144,6 +144,18 @@ describe('style directive', () => {
144
144
matchMedia . activate ( 'xs' ) ;
145
145
expectNativeEl ( fixture , { fontSize : 19 } ) . toHaveStyle ( { 'font-size' : '19px' } , styler ) ;
146
146
} ) ;
147
+
148
+ it ( 'should work with just ngStyle and preexisting styles' , ( ) => {
149
+ createTestComponent ( `
150
+ <div style="background-color: red; height: 100px; width: 100px;" [ngStyle]="divStyle">
151
+ First div
152
+ </div>
153
+ ` ) ;
154
+ expectNativeEl ( fixture ) . toHaveStyle ( { 'background-color' : 'red' } , styler ) ;
155
+ expectNativeEl ( fixture ) . toHaveStyle ( { 'height' : '100px' } , styler ) ;
156
+ expectNativeEl ( fixture ) . toHaveStyle ( { 'width' : '100px' } , styler ) ;
157
+ expectNativeEl ( fixture ) . toHaveStyle ( { 'border' : '2px solid green' } , styler ) ;
158
+ } ) ;
147
159
} ) ;
148
160
149
161
// *****************************************************************
@@ -156,6 +168,7 @@ describe('style directive', () => {
156
168
} )
157
169
class TestStyleComponent {
158
170
fontSize : number = 0 ;
171
+ divStyle = { 'border' : '2px solid green' } ;
159
172
}
160
173
161
174
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ import {
35
35
export class StyleDirective extends BaseDirective2 implements DoCheck {
36
36
37
37
protected DIRECTIVE_KEY = 'ngStyle' ;
38
+ protected fallbackStyles : NgStyleMap = { } ;
38
39
39
40
constructor ( protected elementRef : ElementRef ,
40
41
protected styler : StyleUtils ,
@@ -50,14 +51,13 @@ export class StyleDirective extends BaseDirective2 implements DoCheck {
50
51
this . ngStyleInstance = new NgStyle ( this . keyValueDiffers , this . elementRef , this . renderer ) ;
51
52
}
52
53
this . init ( ) ;
53
- this . setValue ( this . nativeElement . getAttribute ( 'style' ) || '' , '' ) ;
54
+ const styles = this . nativeElement . getAttribute ( 'style' ) || '' ;
55
+ this . fallbackStyles = this . buildStyleMap ( styles ) ;
54
56
}
55
57
56
58
protected updateWithValue ( value : any ) {
57
59
const styles = this . buildStyleMap ( value ) ;
58
- const defaultStyles = this . marshal . getValue ( this . nativeElement , this . DIRECTIVE_KEY , '' ) ;
59
- const fallback = this . buildStyleMap ( defaultStyles ) ;
60
- this . ngStyleInstance . ngStyle = { ...fallback , ...styles } ;
60
+ this . ngStyleInstance . ngStyle = { ...this . fallbackStyles , ...styles } ;
61
61
this . ngStyleInstance . ngDoCheck ( ) ;
62
62
}
63
63
You can’t perform that action at this time.
0 commit comments