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

Commit 6ff682d

Browse files
author
Kent C. Dodds
committed
Adding template property. Expects string.
1 parent 5d4c225 commit 6ff682d

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ When constructing fields use the options below to customize each field object. Y
111111
> [`hidden`](#hidden-form-field),
112112
> [`email`](#email-form-field)
113113
114+
---
115+
##### template (string)
116+
>`template` can be set instead of `type` to use a custom html template form field. Should be used with one-liners mostly (like a directive). Useful for adding functionality to fields.
117+
118+
###### Default
119+
>`undefined`
120+
114121
---
115122
##### templateUrl (string)
116123
>`templateUrl` can be set instead of `type` to use a custom html template form field. Set a path relative to the root of the application. ie `directives/custom-field.html`

src/directives/formly-field.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,26 @@ angular.module('formly.render')
5151
value: '=formValue'
5252
},
5353
link: function fieldLink($scope, $element, $attr) {
54-
var templateUrl = $scope.options.templateUrl || getTemplateUrl($scope.options.type);
55-
if (templateUrl) {
56-
$http.get(templateUrl, {
57-
cache: $templateCache
58-
}).success(function(data) {
59-
//template data returned
60-
$element.html(data);
61-
$compile($element.contents())($scope);
62-
});
54+
var template = $scope.options.template;
55+
if (template) {
56+
setElementTemplate(template);
6357
} else {
64-
console.log('Formly Error: template type \'' + $scope.options.type + '\' not supported.');
58+
var templateUrl = $scope.options.templateUrl || getTemplateUrl($scope.options.type);
59+
if (templateUrl) {
60+
$http.get(templateUrl, {
61+
cache: $templateCache
62+
}).then(function(response) {
63+
setElementTemplate(response.data);
64+
}, function(error) {
65+
console.log('Formly Error: Problem loading template for ' + templateUrl, error);
66+
});
67+
} else {
68+
console.log('Formly Error: template type \'' + $scope.options.type + '\' not supported.');
69+
}
70+
}
71+
function setElementTemplate(templateData) {
72+
$element.html(templateData);
73+
$compile($element.contents())($scope);
6574
}
6675
},
6776
controller: function fieldController($scope) {
@@ -71,7 +80,7 @@ angular.module('formly.render')
7180
}
7281

7382
// set field id to link labels and fields
74-
$scope.id = $scope.formId + $scope.options.type + $scope.index;
83+
$scope.id = $scope.formId + ($scope.options.type || 'Custom') + $scope.index;
7584
}
7685
};
7786
});

src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<script src="bower_components/highlightjs/highlight.pack.js"></script>
3636
<script src="app.js"></script>
3737
<script src="views/home.js"></script>
38+
<script src="views/custom-field.js"></script>
3839
<script src="modules/formly.js"></script>
3940
<script src="modules/formly-render.js"></script>
4041
<script src="directives/formly-form.js"></script>

src/views/home.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ app.controller('home', function($scope, $parse, $rootScope) {
8888
min: 0,
8989
max: 100,
9090
required: true
91+
}, {
92+
key: 'seeWhatYouType',
93+
type: 'customTemplate',
94+
templateUrl: 'views/custom-template.html',
95+
label: 'Do you like seeing what you type?'
96+
}, {
97+
key: 'useDirective',
98+
template: '<div custom-field add-smile="true"></div>',
99+
type: 'customField',
100+
label: 'Do you want the power?'
91101
}, {
92102
key: 'transportation',
93103
type: 'select',

0 commit comments

Comments
 (0)