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

Commit 917ec26

Browse files
author
Kent C. Dodds
committed
Merge pull request #570 from MCKRUZ/master
fix(formly-field): Watch does not work on deep properties #542
2 parents 61db46e + 2b69b8e commit 917ec26

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/directives/formly-field.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,27 @@ describe('formly-field', function() {
15481548
expect(expressionPropertySpy).to.have.been.calledOnce
15491549
})
15501550

1551+
it(`should add watches on deep dive fields`, () => {
1552+
const formWithOptions = '<formly-form model="model" fields="fields" options="options"></formly-form>'
1553+
scope.model = {}
1554+
scope.options = {}
1555+
1556+
const deepLinkField = getNewField()
1557+
deepLinkField.key = 'foo.bar'
1558+
deepLinkField.watcher = {
1559+
listener: sinon.spy(),
1560+
}
1561+
1562+
scope.fields = [deepLinkField]
1563+
compileAndDigest(formWithOptions)
1564+
expect(deepLinkField.watcher.listener).to.have.been.called
1565+
scope.model.foo = {
1566+
bar: 'brown',
1567+
}
1568+
scope.$digest()
1569+
expect(deepLinkField.watcher.listener).to.have.been.called
1570+
})
1571+
15511572
it('should make original model available on field scope, even another model has been set for field', () => {
15521573

15531574
scope.model = {foo: 'bar', child: {fox: 'jumps'}}

src/directives/formly-form.js

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

292292
function getWatchExpression(watcher, field, index) {
293-
let watchExpression = watcher.expression || `model['${field.key}']`
293+
let watchExpression = watcher.expression || 'model[\'' + field.key.toString().split('.').join('\'][\'') + '\']'
294294
if (angular.isFunction(watchExpression)) {
295295
// wrap the field's watch expression so we can call it with the field as the first arg
296296
// and the stop function as the last arg as a helper

0 commit comments

Comments
 (0)