@@ -77,9 +77,41 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
77
77
return undefined
78
78
}
79
79
if ( angular . isDefined ( newVal ) ) {
80
- $scope . model [ $scope . options . key ] = newVal
80
+ parseSet ( $scope . options . key , $scope . model , newVal )
81
+ }
82
+ return parseGet ( $scope . options . key , $scope . model )
83
+ }
84
+
85
+ function parseSet ( key , model , newVal ) {
86
+ // If either of these are null/undefined then just return undefined
87
+ if ( ! key || ! model ) {
88
+ return
89
+ }
90
+ // If we are working with a number then $parse wont work, default back to the old way for now
91
+ if ( angular . isNumber ( key ) ) {
92
+ // TODO: Fix this so we can get several levels instead of just one with properties that are numeric
93
+ model [ key ] = newVal
94
+ } else {
95
+ const setter = $parse ( $scope . options . key ) . assign
96
+ if ( setter ) {
97
+ setter ( $scope . model , newVal )
98
+ }
99
+ }
100
+ }
101
+
102
+ function parseGet ( key , model ) {
103
+ // If either of these are null/undefined then just return undefined
104
+ if ( ! key || ! model ) {
105
+ return undefined
106
+ }
107
+
108
+ // If we are working with a number then $parse wont work, default back to the old way for now
109
+ if ( angular . isNumber ( key ) ) {
110
+ // TODO: Fix this so we can get several levels instead of just one with properties that are numeric
111
+ return model [ key ]
112
+ } else {
113
+ return $parse ( key ) ( model )
81
114
}
82
- return $scope . model [ $scope . options . key ]
83
115
}
84
116
85
117
function simplifyLife ( options ) {
@@ -109,14 +141,14 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
109
141
}
110
142
111
143
function setDefaultValue ( ) {
112
- if ( angular . isDefined ( $scope . options . defaultValue ) && ! angular . isDefined ( $scope . model [ $scope . options . key ] ) ) {
113
- const setter = $parse ( $scope . options . key ) . assign
114
- setter ( $scope . model , $scope . options . defaultValue )
144
+ if ( angular . isDefined ( $scope . options . defaultValue ) &&
145
+ ! angular . isDefined ( parseGet ( $scope . options . key , $scope . model ) ) ) {
146
+ parseSet ( $scope . options . key , $scope . model , $scope . options . defaultValue )
115
147
}
116
148
}
117
149
118
150
function setInitialValue ( ) {
119
- $scope . options . initialValue = $scope . model && $scope . model [ $scope . options . key ]
151
+ $scope . options . initialValue = $scope . model && parseGet ( $scope . options . key , $scope . model )
120
152
}
121
153
122
154
function mergeFieldOptionsWithTypeDefaults ( options , type ) {
@@ -151,7 +183,7 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
151
183
}
152
184
153
185
function resetModel ( ) {
154
- $scope . model [ $scope . options . key ] = $scope . options . initialValue
186
+ parseSet ( $scope . options . key , $scope . model , $scope . options . initialValue )
155
187
if ( $scope . options . formControl ) {
156
188
if ( angular . isArray ( $scope . options . formControl ) ) {
157
189
angular . forEach ( $scope . options . formControl , function ( formControl ) {
@@ -165,7 +197,7 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
165
197
166
198
function resetFormControl ( formControl , isMultiNgModel ) {
167
199
if ( ! isMultiNgModel ) {
168
- formControl . $setViewValue ( $scope . model [ $scope . options . key ] )
200
+ formControl . $setViewValue ( parseGet ( $scope . options . key , $scope . model ) )
169
201
}
170
202
171
203
formControl . $render ( )
@@ -179,7 +211,7 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
179
211
}
180
212
181
213
function updateInitialValue ( ) {
182
- $scope . options . initialValue = $scope . model [ $scope . options . key ]
214
+ $scope . options . initialValue = parseGet ( $scope . options . key , $scope . model )
183
215
}
184
216
185
217
function addValidationMessages ( options ) {
0 commit comments