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

Commit 1e633e7

Browse files
author
Kent C. Dodds
committed
Adding "hiddenExpression" to make it easier to interface with the hide feature
1 parent b57e9b6 commit 1e633e7

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Version numbers correspond to `bower.json` version
44

55
## Features
66

7-
Added `hide` property on fields. Allows devs to conditionally show fields.
7+
Added `hide` property on fields. Allows devs to conditionally show fields. Also adding `hideExpression` property which is an expression that will be evaluated on the result object (using $parse) to make the api simpler to use.
88

99
## Bug Fixes
1010

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Example data as it would be set in the controller
5757
label: 'Password',
5858
required: true,
5959
disabled: false, //default: false
60-
hide: true, // see watch below
60+
hideExpression: '!username' // hide when username is blank
6161
}
6262

6363
];
@@ -79,10 +79,6 @@ Example data as it would be set in the controller
7979
$scope.onSubmit = function() {
8080
console.log('form submitted:', $scope.formData);
8181
};
82-
83-
$scope.$watch('!formData.username', function(isBlank) {
84-
$scope.formFields[1].hide = !isBlank;
85-
});
8682
```
8783
### Creating Forms
8884
Forms can be customized with the options below.
@@ -144,9 +140,16 @@ When constructing fields use the options below to customize each field object. Y
144140
###### Default
145141
>`undefined`
146142
143+
---
144+
##### hideExpression (expression string)
145+
>`hideExpression` is used to conditionally show the input. Evaluates on the `result` and uses the `hide` property on the field.
146+
147+
###### Default
148+
>`undefined`
149+
147150
---
148151
##### hide (boolean)
149-
>`hide` when true, the input is hidden (meant to be used with a watch). In the example above, the password field would be hidden until username has a value.
152+
>`hide` is used to conditionally show the input. When true, the input is hidden (meant to be used with a watch).
150153
151154
###### Default
152155
>`undefined`

src/directives/formly-form.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
angular.module('formly.render')
3-
.directive('formlyForm', function formlyForm() {
3+
.directive('formlyForm', function formlyForm($parse) {
44
return {
55
restrict: 'AE',
66
templateUrl: 'directives/formly-form.html',
@@ -17,6 +17,16 @@ angular.module('formly.render')
1717
//Post gets called after angular has created the FormController
1818
//Now pass the FormController back up to the parent scope
1919
scope.formOnParentScope = scope[attr.name];
20+
angular.forEach(scope.fields, function(field) {
21+
if (field.hideExpression) {
22+
var getter = $parse(field.hideExpression);
23+
scope.$watch(function() {
24+
return getter(scope.result);
25+
}, function(hide) {
26+
field.hide = hide;
27+
});
28+
}
29+
});
2030
}
2131
}
2232
}

0 commit comments

Comments
 (0)