@@ -16,6 +16,8 @@ import {
16
16
ViewEncapsulation ,
17
17
Inject ,
18
18
Optional ,
19
+ ChangeDetectionStrategy ,
20
+ ChangeDetectorRef ,
19
21
} from '@angular/core' ;
20
22
import { ENTER , SPACE } from '../keyboard/keycodes' ;
21
23
import { coerceBooleanProperty } from '@angular/cdk' ;
@@ -55,19 +57,27 @@ export class MdOptionSelectionChange {
55
57
'class' : 'mat-option' ,
56
58
} ,
57
59
templateUrl : 'option.html' ,
58
- encapsulation : ViewEncapsulation . None
60
+ encapsulation : ViewEncapsulation . None ,
61
+ changeDetection : ChangeDetectionStrategy . OnPush ,
59
62
} )
60
63
export class MdOption {
61
64
private _selected : boolean = false ;
62
65
private _active : boolean = false ;
66
+ private _multiple : boolean = false ;
63
67
64
68
/** Whether the option is disabled. */
65
69
private _disabled : boolean = false ;
66
70
67
71
private _id : string = `md-option-${ _uniqueIdCounter ++ } ` ;
68
72
69
73
/** Whether the wrapping component is in multiple selection mode. */
70
- multiple : boolean = false ;
74
+ get multiple ( ) { return this . _multiple ; }
75
+ set multiple ( value : boolean ) {
76
+ if ( value !== this . _multiple ) {
77
+ this . _multiple = value ;
78
+ this . _changeDetectorRef . markForCheck ( ) ;
79
+ }
80
+ }
71
81
72
82
/** The unique ID of the option. */
73
83
get id ( ) { return this . _id ; }
@@ -88,6 +98,7 @@ export class MdOption {
88
98
89
99
constructor (
90
100
private _element : ElementRef ,
101
+ private _changeDetectorRef : ChangeDetectorRef ,
91
102
@Optional ( ) public readonly group : MdOptgroup ,
92
103
@Optional ( ) @Inject ( MATERIAL_COMPATIBILITY_MODE ) public _isCompatibilityMode : boolean ) { }
93
104
@@ -113,12 +124,14 @@ export class MdOption {
113
124
/** Selects the option. */
114
125
select ( ) : void {
115
126
this . _selected = true ;
127
+ this . _changeDetectorRef . markForCheck ( ) ;
116
128
this . _emitSelectionChangeEvent ( ) ;
117
129
}
118
130
119
131
/** Deselects the option. */
120
132
deselect ( ) : void {
121
133
this . _selected = false ;
134
+ this . _changeDetectorRef . markForCheck ( ) ;
122
135
this . _emitSelectionChangeEvent ( ) ;
123
136
}
124
137
@@ -133,7 +146,10 @@ export class MdOption {
133
146
* events will display the proper options as active on arrow key events.
134
147
*/
135
148
setActiveStyles ( ) : void {
136
- this . _active = true ;
149
+ if ( ! this . _active ) {
150
+ this . _active = true ;
151
+ this . _changeDetectorRef . markForCheck ( ) ;
152
+ }
137
153
}
138
154
139
155
/**
@@ -142,7 +158,10 @@ export class MdOption {
142
158
* events will display the proper options as active on arrow key events.
143
159
*/
144
160
setInactiveStyles ( ) : void {
145
- this . _active = false ;
161
+ if ( this . _active ) {
162
+ this . _active = false ;
163
+ this . _changeDetectorRef . markForCheck ( ) ;
164
+ }
146
165
}
147
166
148
167
/** Ensures the option is selected when activated from the keyboard. */
@@ -162,6 +181,7 @@ export class MdOption {
162
181
_selectViaInteraction ( ) : void {
163
182
if ( ! this . disabled ) {
164
183
this . _selected = this . multiple ? ! this . _selected : true ;
184
+ this . _changeDetectorRef . markForCheck ( ) ;
165
185
this . _emitSelectionChangeEvent ( true ) ;
166
186
}
167
187
}
0 commit comments