Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit f8bc547

Browse files
committed
1 parent 967e9ce commit f8bc547

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

src/directives/formly-form.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
290290
}
291291

292292
function setupWatchers(field, index) {
293-
if (isFieldGroup(field) || !angular.isDefined(field.watcher)) {
293+
if (!angular.isDefined(field.watcher)) {
294294
return
295295
}
296296
let watchers = field.watcher
@@ -313,7 +313,12 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
313313
}
314314

315315
function getWatchExpression(watcher, field, index) {
316-
let watchExpression = watcher.expression || 'model[\'' + field.key.toString().split('.').join('\'][\'') + '\']'
316+
let watchExpression
317+
if (!angular.isUndefined(watcher.expression)) {
318+
watchExpression = watcher.expression
319+
} else if (field.key) {
320+
watchExpression = 'model[\'' + field.key.toString().split('.').join('\'][\'') + '\']'
321+
}
317322
if (angular.isFunction(watchExpression)) {
318323
// wrap the field's watch expression so we can call it with the field as the first arg
319324
// and the stop function as the last arg as a helper

src/directives/formly-form.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,31 @@ describe('formly-form', () => {
952952
expect(listener).to.have.been.called
953953
expect(expression).to.have.been.called
954954
})
955+
956+
it(`should setup any watchers specified on a fieldgroup`, () => {
957+
scope.model = {}
958+
959+
const listener = sinon.spy()
960+
const expression = sinon.spy()
961+
962+
scope.fields = [{
963+
watcher: [{
964+
listener: '',
965+
expression: '',
966+
}, {
967+
listener,
968+
expression,
969+
}],
970+
fieldGroup: [
971+
getNewField({}),
972+
getNewField({}),
973+
],
974+
}]
975+
976+
expect(compileAndDigest).to.not.throw()
977+
expect(listener).to.have.been.called
978+
expect(expression).to.have.been.called
979+
})
955980
})
956981

957982
describe(`manualModelWatcher option`, () => {

src/providers/formlyApiCheck.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ const validatorChecker = apiCheck.objectOf(apiCheck.oneOfType([
7777
}).strict,
7878
]))
7979

80+
const watcherChecker = apiCheck.typeOrArrayOf(
81+
apiCheck.shape({
82+
expression: formlyExpression.optional,
83+
listener: formlyExpression.optional,
84+
runFieldExpressions: apiCheck.bool.optional,
85+
})
86+
)
87+
8088
const fieldOptionsApiShape = {
8189
$$hashKey: apiCheck.any.optional,
8290
type: apiCheck.shape.ifNot(['template', 'templateUrl'], apiCheck.string).optional,
@@ -113,13 +121,7 @@ const fieldOptionsApiShape = {
113121
getterSetter: apiCheck.bool.optional,
114122
timezone: apiCheck.string.optional,
115123
}).optional,
116-
watcher: apiCheck.typeOrArrayOf(
117-
apiCheck.shape({
118-
expression: formlyExpression.optional,
119-
listener: formlyExpression.optional,
120-
runFieldExpressions: apiCheck.bool.optional,
121-
})
122-
).optional,
124+
watcher: watcherChecker.optional,
123125
validators: validatorChecker.optional,
124126
asyncValidators: validatorChecker.optional,
125127
parsers: apiCheck.arrayOf(formlyExpression).optional,
@@ -184,6 +186,7 @@ const fieldGroup = apiCheck.shape({
184186
options: formOptionsApi.optional,
185187
templateOptions: apiCheck.object.optional,
186188
wrapper: specifyWrapperType.optional,
189+
watcher: watcherChecker.optional,
187190
hide: apiCheck.bool.optional,
188191
hideExpression: formlyExpression.optional,
189192
data: apiCheck.object.optional,

0 commit comments

Comments
 (0)