@@ -92,11 +92,30 @@ angular.module('formly.render').directive('formlyForm', [
92
92
'$element' ,
93
93
'$parse' ,
94
94
function ( $scope , $element , $parse ) {
95
+ // setup watches for watchExpressions
96
+ angular . forEach ( $scope . fields , function ( field , index ) {
97
+ if ( angular . isDefined ( field . watch ) ) {
98
+ var watchExpression = field . watch . expression ;
99
+ if ( angular . isFunction ( watchExpression ) ) {
100
+ // wrap the field's watch expression so we can call it with the field as the first arg as a helper
101
+ watchExpression = function ( ) {
102
+ var args = Array . prototype . slice . call ( arguments , 0 ) ;
103
+ args . unshift ( field ) ;
104
+ return field . watch . expression . apply ( this , args ) ;
105
+ } ;
106
+ }
107
+ $scope . $watch ( watchExpression , function ( ) {
108
+ // wrap the field's watch listener so we can call it with the field as the first arg as a helper
109
+ var args = Array . prototype . slice . call ( arguments , 0 ) ;
110
+ args . unshift ( field ) ;
111
+ return field . watch . listener . apply ( this , args ) ;
112
+ } ) ;
113
+ }
114
+ } ) ;
95
115
$scope . $watch ( 'result' , function ( newValue ) {
96
116
angular . forEach ( $scope . fields , function ( field , index ) {
97
117
if ( field . hideExpression ) {
98
- var getter = $parse ( field . hideExpression ) ;
99
- field . hide = getter ( $scope . result ) ;
118
+ field . hide = $parse ( field . hideExpression ) ( $scope . result ) ;
100
119
}
101
120
} ) ;
102
121
} , true ) ;
@@ -168,14 +187,14 @@ angular.module('formly.render').run([
168
187
'$templateCache' ,
169
188
function ( $templateCache ) {
170
189
'use strict' ;
171
- $templateCache . put ( 'directives/formly-field-checkbox.html' , '<div class=checkbox><label><input type=checkbox ng-required=options.required ng-disabled=options.disabled ng-model=value>{{options.label || \'Checkbox\'}} {{options.required ? \'*\' : \'\'}}</label></div>' ) ;
190
+ $templateCache . put ( 'directives/formly-field-checkbox.html' , '<div class=checkbox><label><input type=checkbox ng-required=options.required ng-disabled=options.disabled ng-model=value> {{options.label || \'Checkbox\'}} {{options.required ? \'*\' : \'\'}}</label></div>' ) ;
172
191
$templateCache . put ( 'directives/formly-field-email.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Email\'}} {{options.required ? \'*\' : \'\'}}</label><input type=email class=form-control id={{id}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled ng-model=value></div>' ) ;
173
192
$templateCache . put ( 'directives/formly-field-hidden.html' , '<input type=hidden ng-model=value>' ) ;
174
193
$templateCache . put ( 'directives/formly-field-number.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Number\'}} {{options.required ? \'*\' : \'\'}}</label><input type=number class=form-control id={{id}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled min={{options.min}} max={{options.max}} ng-minlength={{options.minlength}} ng-maxlength={{options.maxlength}} ng-model=value></div>' ) ;
175
194
$templateCache . put ( 'directives/formly-field-password.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Password\'}} {{options.required ? \'*\' : \'\'}}</label><input type=password class=form-control id={{id}} ng-required=options.required ng-disabled=options.disabled ng-model=value></div>' ) ;
176
- $templateCache . put ( 'directives/formly-field-radio.html' , '<div class=radio-group><label class=control-label>{{options.label}} {{options.required ? \'*\' : \'\'}}</label><div class=radio ng-repeat="(key, option) in options.options"><label><input type=radio name={{id}} id="{{id + \'_\'+ $index}}" ng-value=option.value ng-required=options.required ng-model=$parent.value>{{option.name}}</label></div></div>' ) ;
195
+ $templateCache . put ( 'directives/formly-field-radio.html' , '<div class=radio-group><label class=control-label>{{options.label}} {{options.required ? \'*\' : \'\'}}</label><div class=radio ng-repeat="(key, option) in options.options"><label><input type=radio name={{id}} id="{{id + \'_\'+ $index}}" ng-value=option.value ng-required=options.required ng-model=$parent.value> {{option.name}}</label></div></div>' ) ;
177
196
$templateCache . put ( 'directives/formly-field-select.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Select\'}} {{options.required ? \'*\' : \'\'}}</label><select class=form-control id={{id}} ng-model=value ng-required=options.required ng-disabled=options.disabled ng-init="value = options.options[options.default]" ng-options="option.name group by option.group for option in options.options"></select></div>' ) ;
178
- $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>' ) ;
197
+ $templateCache . put ( 'directives/formly-field-text.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Text\'}} {{options.required ? \'*\' : \'\'}}</label><input type=text class=form-control id={{id}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled ng-model=value></div>' ) ;
179
198
$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>' ) ;
180
199
$templateCache . put ( 'directives/formly-field.html' , '' ) ;
181
200
$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 ng-hide=field.hide></formly-field><button type=submit class="btn btn-primary" ng-hide=options.hideSubmit>{{options.submitCopy || "Submit"}}</button></form>' ) ;
0 commit comments