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

Commit 8f87532

Browse files
author
Kent C. Dodds
committed
Closes #37. Allows people to reduce their watch count. Changed how the template works for a formly-form a little bit...
1 parent 0241d1c commit 8f87532

File tree

7 files changed

+45
-21
lines changed

7 files changed

+45
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Version numbers correspond to `bower.json` version
33

44
- Now providing the `result` to each field template authors have the power to associate other values of the result set with the template
55
- Adding `requiredExpression` to field options and assinging the field's `required` property based on its evaluation. Works very much like `hideExpression`
6+
- Added option to use `ng-if` instead of `ng-hide` to hide fields.
67

78
# 0.0.14
89

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,21 +473,29 @@ formlyTemplateProvider.getTemplateUrl('radio') === 'views/custom-formly-radio.ht
473473

474474
#### formlyOptionsProvider
475475

476-
You can configure default options for all forms using the `formlyOptionsProvider` in your app's `config` function. This has the following api:
476+
You can configure default options for all forms using the `formlyOptionsProvider` in your app's `config` function. The following options are used by angular-formly and are available for configuration:
477+
478+
- uniqueFormId - not useful for global configuration, but useful on a per-form basis. Defaults to null
479+
- submitCopy - what the submit button should say. Defaults to "Submit"
480+
- hideSubmit - whether to hide the submit button. Defaults to false
481+
- submitButtonTemplate - a custom template for the submit button. Defaults to null
482+
- useNgIfToHide - whether to use `ng-if` for hiding fields (rather than `ng-hide`). Useful for removing watchers. Defaults to false (use `ng-hide`)
483+
484+
To change these defaults globally, you have the following api:
477485

478486
##### setOption
479487

480488
Allows you to set an option
481489

482490
```javascript
483-
formlyOptionsProvider.setOption('uniqueFormId', 'probablyDontWantToDoThis');
491+
formlyOptionsProvider.setOption('useNgIfToHide', true);
484492
formlyOptionsProvider.setOption('submitCopy', 'Save');
485493

486494
// the same can be accomplished with
487495

488496
formlyOptionsProvider.setOption({
489497
submitCopy: 'Save',
490-
uniqueFormId: 'probablyDontWantToDoThis'
498+
useNgIfToHide: true
491499
});
492500
```
493501

src/directives/formly-field.html

Whitespace-only changes.

src/directives/formly-form.html

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/directives/formly-form.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,50 @@
11
'use strict';
22
angular.module('formly.render')
33
.directive('formlyForm', function formlyForm(formlyOptions, $compile) {
4+
var templateHide = 'ng-hide="field.hide"';
5+
var fieldsTemplate = [
6+
'<formly-field ng-repeat="field in fields"',
7+
'options="field"',
8+
'form-result="result"',
9+
'form-value="result[field.key||$index]"',
10+
'form-id="options.uniqueFormId"',
11+
'ng-hide="field.hide"',
12+
'index="$index">',
13+
'</formly-field>'
14+
].join(' ')
415
return {
516
restrict: 'AE',
6-
templateUrl: 'directives/formly-form.html',
17+
template: function(el, attr) {
18+
var useNgIf = formlyOptions.getOptions().useNgIfToHide;
19+
return [
20+
'<form class="formly" role="form">',
21+
'<div class="ng-hide">fields</div>',
22+
'<button type="submit"',
23+
'ng-show="!options.hideSubmit">',
24+
'{{options.submitCopy || "Submit"}}',
25+
'</button>',
26+
'</form>'
27+
].join(' ');
28+
},
729
replace: true,
830
scope: {
931
fields: '=',
1032
options: '=?',
1133
result: '=',
1234
formOnParentScope: '=name'
1335
},
14-
compile: function (scope, iElement, iAttrs, controller, transcludeFn) {
36+
compile: function () {
1537
return {
1638
post: function (scope, ele, attr, controller) {
1739
scope.options = angular.extend(formlyOptions.getOptions(), scope.options);
1840
if (scope.options.submitButtonTemplate) {
1941
ele.find('button').replaceWith($compile(scope.options.submitButtonTemplate)(scope));
2042
}
43+
var template = fieldsTemplate;
44+
if (scope.options.useNgIfToHide) {
45+
template = template.replace(templateHide, 'ng-if="!field.hide"');
46+
}
47+
ele.find('div').replaceWith($compile(template)(scope));
2148
//Post gets called after angular has created the FormController
2249
//Now pass the FormController back up to the parent scope
2350
scope.formOnParentScope = scope[attr.name];

src/providers/formly-options.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ angular.module('formly.render')
66
uniqueFormId: null,
77
submitCopy: "Submit",
88
hideSubmit: false,
9-
submitButtonTemplate: null
9+
submitButtonTemplate: null,
10+
useNgIfToHide: false
1011
};
1112

1213
function setOption(name, value) {

src/views/home.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ app.controller('home', function($scope, $parse, formlyOptions, $window, usingCus
204204
$scope.formOptions = {
205205
uniqueFormId: 'formly',
206206
submitCopy: 'Save',
207-
hideSubmit: false
207+
hideSubmit: false,
208+
useNgIfToHide: true
208209
};
209210
$scope.hiddenFormOptions = {
210211
uniqueFormId: 'hiddenFormly',

0 commit comments

Comments
 (0)