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

Commit 9f426e8

Browse files
committed
added a key option to specify the key for the result data object
1 parent 634dc26 commit 9f426e8

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/directives/formly-form.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,24 @@ angular.module('formly.render')
1616
$scope.populateResult = function() {
1717
var formChildren = $element.children();
1818
var fieldScope;
19-
angular.forEach(formChildren, function(field, key){
19+
angular.forEach(formChildren, function(fieldElement, key){
2020
// grab fields isolate scope
21-
fieldScope = angular.element(field).scope();
22-
$scope.result[fieldScope.$index] = fieldScope.value;
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+
}
2337
});
2438
};
2539
}

src/views/home.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ app.controller('home', function($scope, $parse, $rootScope) {
3838
// Public Vars
3939
$scope.formFields = [{
4040
type: 'email',
41-
placeholder: '[email protected]'
41+
placeholder: '[email protected]',
42+
key: 'email'
4243
}, {
4344
type: 'text',
4445
label: 'First Name',

0 commit comments

Comments
 (0)