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

Commit 776e6e5

Browse files
author
Kent C. Dodds
committed
Improving hideExpression capabilities. hideExpressions can now be added to fields dynamically.
1 parent 414cc34 commit 776e6e5

File tree

5 files changed

+52
-64
lines changed

5 files changed

+52
-64
lines changed

dist/formly.js

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -80,41 +80,43 @@ angular.module('formly.render').directive('formlyField', [
8080
}
8181
]);
8282
'use strict';
83-
angular.module('formly.render').directive('formlyForm', [
84-
'$parse',
85-
function formlyForm($parse) {
86-
return {
87-
restrict: 'AE',
88-
templateUrl: 'directives/formly-form.html',
89-
replace: true,
90-
scope: {
91-
fields: '=fields',
92-
options: '=options',
93-
result: '=result',
94-
formOnParentScope: '=name'
95-
},
96-
compile: function (scope, iElement, iAttrs, controller, transcludeFn) {
97-
return {
98-
post: function (scope, ele, attr, controller) {
99-
//Post gets called after angular has created the FormController
100-
//Now pass the FormController back up to the parent scope
101-
scope.formOnParentScope = scope[attr.name];
102-
angular.forEach(scope.fields, function (field) {
103-
if (field.hideExpression) {
104-
var getter = $parse(field.hideExpression);
105-
scope.$watch(function () {
106-
return getter(scope.result);
107-
}, function (hide) {
108-
field.hide = hide;
109-
});
110-
}
111-
});
112-
}
113-
};
83+
angular.module('formly.render').directive('formlyForm', function formlyForm() {
84+
return {
85+
restrict: 'AE',
86+
templateUrl: 'directives/formly-form.html',
87+
replace: true,
88+
scope: {
89+
fields: '=fields',
90+
options: '=options',
91+
result: '=result',
92+
formOnParentScope: '=name'
93+
},
94+
compile: function (scope, iElement, iAttrs, controller, transcludeFn) {
95+
return {
96+
post: function (scope, ele, attr, controller) {
97+
//Post gets called after angular has created the FormController
98+
//Now pass the FormController back up to the parent scope
99+
scope.formOnParentScope = scope[attr.name];
100+
}
101+
};
102+
},
103+
controller: [
104+
'$scope',
105+
'$element',
106+
'$parse',
107+
function ($scope, $element, $parse) {
108+
$scope.$watch('result', function (newValue) {
109+
angular.forEach($scope.fields, function (field, index) {
110+
if (field.hideExpression) {
111+
var getter = $parse(field.hideExpression);
112+
field.hide = getter($scope.result);
113+
}
114+
});
115+
}, true);
114116
}
115-
};
116-
}
117-
]);
117+
]
118+
};
119+
});
118120
angular.module('formly.render').run([
119121
'$templateCache',
120122
function ($templateCache) {

dist/formly.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/formly.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directives/formly-form.js

Lines changed: 11 additions & 19 deletions
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($parse) {
3+
.directive('formlyForm', function formlyForm() {
44
return {
55
restrict: 'AE',
66
templateUrl: 'directives/formly-form.html',
@@ -17,26 +17,18 @@ 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-
21-
var watches = {};
22-
angular.forEach(scope.fields, function(field) {
23-
if (field.hideExpression) {
24-
watches[field.hideExpression] = watches[field.hideExpression] || [];
25-
watches[field.hideExpression].push(field);
26-
}
27-
});
28-
angular.forEach(watches, function(fields, expression) {
29-
var getter = $parse(expression);
30-
scope.$watch(function() {
31-
return getter(scope.result);
32-
}, function(hide) {
33-
angular.forEach(fields, function(field) {
34-
field.hide = hide;
35-
});
36-
});
37-
});
3820
}
3921
}
22+
},
23+
controller: function($scope, $element, $parse) {
24+
$scope.$watch('result', function(newValue) {
25+
angular.forEach($scope.fields, function(field, index) {
26+
if (field.hideExpression) {
27+
var getter = $parse(field.hideExpression);
28+
field.hide = getter($scope.result);
29+
}
30+
});
31+
}, true);
4032
}
4133
};
4234
});

src/views/home.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,14 @@ app.controller('home', function($scope, $parse, $rootScope) {
147147
key: 'hiddenWhenUnchecked',
148148
type: 'text',
149149
label: 'Conditional input',
150-
placeholder: 'These both use the same hideExpression...',
151-
hideExpression: '!checkThis'
152-
}, {
153-
key: 'hiddenWhenUnchecked2',
154-
type: 'text',
155-
label: 'Type "joe" in here',
156-
placeholder: '... so there is only one $watch for both of them',
150+
placeholder: 'This is a big secret! Try typing "joe"',
157151
hideExpression: '!checkThis'
158152
}, {
159153
key: 'showWhenJoe',
160154
type: 'text',
161155
label: 'You typed Joe! You found me!',
162-
placeholder: 'Different hideExpression, different $watch',
163-
hideExpression: 'hiddenWhenUnchecked2 !== "joe"'
156+
placeholder: 'hideExpressions are evaluated on the result',
157+
hideExpression: 'hiddenWhenUnchecked !== "joe"'
164158
}, {
165159
key:'secretCode',
166160
type: 'hidden',

0 commit comments

Comments
 (0)