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

Commit 67bbf6c

Browse files
author
Kent C. Dodds
committed
feat(fieldConfig): Add ngModelElAttrs
Add a simpler way to add attributes to the ng-model element dynamically closes #378
1 parent db8b2b7 commit 67bbf6c

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/providers/formlyApiCheck.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ const fieldOptionsApiShape = {
133133
noFormControl: apiCheck.bool.optional,
134134
hide: apiCheck.bool.optional,
135135
hideExpression: formlyExpression.optional,
136+
ngModelElAttrs: apiCheck.objectOf(apiCheck.string).optional,
136137
ngModelAttrs: apiCheck.objectOf(apiCheck.shape({
137138
expression: apiCheck.shape.ifNot(['value', 'attribute', 'bound'], apiCheck.any).optional,
138139
value: apiCheck.shape.ifNot('expression', apiCheck.any).optional,

src/run/formlyNgModelAttrsManipulator.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function addFormlyNgModelAttrsManipulator(formlyConfig, $interpolate, formlyWarn
2929
addValidation();
3030
addModelOptions();
3131
addTemplateOptionsAttrs();
32+
addNgModelElAttrs();
3233

3334

3435
return node.innerHTML;
@@ -117,6 +118,12 @@ function addFormlyNgModelAttrsManipulator(formlyConfig, $interpolate, formlyWarn
117118
}
118119
});
119120
}
121+
122+
function addNgModelElAttrs() {
123+
angular.forEach(options.ngModelElAttrs, (val, name) => {
124+
addIfNotPresent(modelNodes, name, val);
125+
});
126+
}
120127
}
121128

122129
// Utility functions

src/run/formlyNgModelAttrsManipulator.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,43 @@ describe('formlyNgModelAttrsManipulator', () => {
316316
});
317317

318318

319+
describe(`ngModelElAttrs`, () => {
320+
321+
it(`should place the attributes you specify on the ng-model element`, () => {
322+
_.assign(field, {
323+
ngModelElAttrs: {foo: '{{::to.bar}}'}
324+
});
325+
manipulate();
326+
expect(
327+
resultNode.getAttribute('foo'),
328+
'foo attribute should equal the value of foo in ngModelElAttrs'
329+
).to.equal('{{::to.bar}}');
330+
});
331+
332+
it(`should work with multiple ng-models`, () => {
333+
_.assign(field, {
334+
ngModelElAttrs: {foo: '{{::to.bar}}'}
335+
});
336+
manipulate(`
337+
<div>
338+
<input class="first" ng-model="foo" />
339+
<input class="second" ng-model="bar" />
340+
</div>
341+
`);
342+
expect(
343+
resultNode.querySelector('.first').getAttribute('foo'),
344+
'foo attribute should equal the value of foo in ngModelElAttrs'
345+
).to.equal('{{::to.bar}}');
346+
expect(
347+
resultNode.querySelector('.second').getAttribute('foo'),
348+
'foo attribute should equal the value of foo in ngModelElAttrs'
349+
).to.equal('{{::to.bar}}');
350+
351+
});
352+
});
353+
354+
355+
319356
function manipulate(theTemplate = template) {
320357
result = manipulator(theTemplate, field, scope);
321358
resultEl = angular.element(result);

0 commit comments

Comments
 (0)