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

Commit 00bcf74

Browse files
committed
Added 2 way binding between form values and result=formData
Added access to formobj in the parent scope by passing setting name attr on formly-form
1 parent 4134197 commit 00bcf74

File tree

7 files changed

+83
-66
lines changed

7 files changed

+83
-66
lines changed

dist/formly.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,35 +89,24 @@ angular.module('formly.render').directive('formlyForm', function formlyForm() {
8989
formId: '@formId',
9090
fields: '=fields',
9191
options: '=options',
92-
result: '=result'
92+
result: '=result',
93+
formOnParentScope: '=name'
9394
},
9495
controller: [
9596
'$scope',
9697
'$element',
9798
function formController($scope, $element) {
98-
$scope.populateResult = function () {
99-
var formChildren = $element.children();
100-
var fieldScope;
101-
angular.forEach(formChildren, function (fieldElement, key) {
102-
// grab fields isolate scope
103-
fieldScope = angular.element(fieldElement).scope();
104-
// check if its a form field, otherwise ignore, ie its the button
105-
if (fieldScope.field) {
106-
// if a key is set, then save the data with that key in the result object
107-
// otherwise use the field's index from the fields array
108-
var dataKey;
109-
if ('key' in fieldScope.field) {
110-
dataKey = fieldScope.field.key;
111-
} else {
112-
dataKey = fieldScope.$index;
113-
}
114-
// set value in result
115-
$scope.result[dataKey] = fieldScope.value;
116-
}
117-
});
118-
};
11999
}
120-
]
100+
],
101+
compile: function (scope, iElement, iAttrs, controller, transcludeFn) {
102+
return {
103+
post: function (scope, ele, attr, controller) {
104+
//Post gets called after angular has created the FormController
105+
//Now pass the FormController back up to the parent scope
106+
scope.formOnParentScope = scope[attr.name];
107+
}
108+
};
109+
}
121110
};
122111
});
123112
angular.module('formly.render').run([
@@ -134,6 +123,6 @@ angular.module('formly.render').run([
134123
$templateCache.put('directives/formly-field-text.html', '<div class=form-group><label for={{id}}>{{options.label || \'Text\'}} {{options.required ? \'*\' : \'\'}}</label><input class=form-control id={{id}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled ng-model=value></div>');
135124
$templateCache.put('directives/formly-field-textarea.html', '<div class=form-group><label for={{id}}>{{options.label || \'Text\'}} {{options.required ? \'*\' : \'\'}}</label><textarea type=text class=form-control id={{id}} rows={{options.lines}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled ng-model=value>\n' + '\t</textarea></div>');
136125
$templateCache.put('directives/formly-field.html', '');
137-
$templateCache.put('directives/formly-form.html', '<form class=formly role=form name={{options.uniqueFormId}}><formly-field ng-repeat="field in fields" options=field form-value=value class=formly-field form-id={{options.uniqueFormId}} index={{$index}}></formly-field><button type=submit ng-hide=options.hideSubmit ng-click=populateResult()>{{options.submitCopy || "Submit"}}</button></form>');
126+
$templateCache.put('directives/formly-form.html', '<form class=formly role=form><formly-field ng-repeat="field in fields" options=field form-value=result[field.key||$index] class=formly-field form-id={{options.uniqueFormId}} index={{$index}}></formly-field><button type=submit ng-hide=options.hideSubmit>{{options.submitCopy || "Submit"}}</button></form>');
138127
}
139128
]);

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.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
<form class="formly" role="form" name="{{options.uniqueFormId}}">
1+
<form class="formly" role="form">
22
<formly-field ng-repeat="field in fields"
33
options="field"
4-
form-value="value"
4+
form-value="result[field.key||$index]"
55
class="formly-field"
66
form-id="{{options.uniqueFormId}}"
77
index="{{$index}}">
88
</formly-field>
9-
<button type="submit"
10-
ng-hide="options.hideSubmit"
11-
ng-click="populateResult()">
9+
<button type="submit" ng-hide="options.hideSubmit">
1210
{{options.submitCopy || "Submit"}}
1311
</button>
1412
</form>

src/directives/formly-form.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,19 @@ angular.module('formly.render')
99
formId: '@formId',
1010
fields: '=fields',
1111
options: '=options',
12-
result: '=result'
12+
result: '=result',
13+
formOnParentScope: '=name'
1314
},
1415
controller: function formController($scope, $element) {
15-
16-
$scope.populateResult = function() {
17-
var formChildren = $element.children();
18-
var fieldScope;
19-
angular.forEach(formChildren, function(fieldElement, key){
20-
// grab fields isolate scope
21-
fieldScope = angular.element(fieldElement).scope();
22-
23-
// check if its a form field, otherwise ignore, ie its the button
24-
if (fieldScope.field) {
25-
// if a key is set, then save the data with that key in the result object
26-
// otherwise use the field's index from the fields array
27-
var dataKey;
28-
if('key' in fieldScope.field) {
29-
dataKey = fieldScope.field.key;
30-
} else {
31-
dataKey = fieldScope.$index;
32-
}
33-
34-
// set value in result
35-
$scope.result[dataKey] = fieldScope.value;
36-
}
37-
});
38-
};
39-
}
16+
},
17+
compile: function (scope, iElement, iAttrs, controller, transcludeFn) {
18+
return {
19+
post: function (scope, ele, attr, controller) {
20+
//Post gets called after angular has created the FormController
21+
//Now pass the FormController back up to the parent scope
22+
scope.formOnParentScope = scope[attr.name];
23+
}
24+
}
25+
}
4026
};
4127
});

0 commit comments

Comments
 (0)