@@ -60,41 +60,76 @@ angular.module('formly.render').directive('formlyField', [
60
60
}
61
61
] ) ;
62
62
'use strict' ;
63
- angular . module ( 'formly.render' ) . directive ( 'formlyForm' , function formlyForm ( ) {
64
- return {
65
- restrict : 'AE' ,
66
- templateUrl : 'directives/formly-form.html' ,
67
- replace : true ,
68
- scope : {
69
- fields : '=fields' ,
70
- options : '=options' ,
71
- result : '=result' ,
72
- formOnParentScope : '=name'
73
- } ,
74
- compile : function ( scope , iElement , iAttrs , controller , transcludeFn ) {
75
- return {
76
- post : function ( scope , ele , attr , controller ) {
77
- //Post gets called after angular has created the FormController
78
- //Now pass the FormController back up to the parent scope
79
- scope . formOnParentScope = scope [ attr . name ] ;
80
- }
81
- } ;
82
- } ,
83
- controller : [
84
- '$scope' ,
85
- '$element' ,
86
- '$parse' ,
87
- function ( $scope , $element , $parse ) {
88
- $scope . $watch ( 'result' , function ( newValue ) {
89
- angular . forEach ( $scope . fields , function ( field , index ) {
90
- if ( field . hideExpression ) {
91
- var getter = $parse ( field . hideExpression ) ;
92
- field . hide = getter ( $scope . result ) ;
63
+ angular . module ( 'formly.render' ) . directive ( 'formlyForm' , [
64
+ 'formlyOptions' ,
65
+ '$compile' ,
66
+ function formlyForm ( formlyOptions , $compile ) {
67
+ return {
68
+ restrict : 'AE' ,
69
+ templateUrl : 'directives/formly-form.html' ,
70
+ replace : true ,
71
+ scope : {
72
+ fields : '=' ,
73
+ options : '=?' ,
74
+ result : '=' ,
75
+ formOnParentScope : '=name'
76
+ } ,
77
+ compile : function ( scope , iElement , iAttrs , controller , transcludeFn ) {
78
+ return {
79
+ post : function ( scope , ele , attr , controller ) {
80
+ scope . options = angular . extend ( formlyOptions . getOptions ( ) , scope . options ) ;
81
+ if ( scope . options . submitButtonTemplate ) {
82
+ ele . find ( 'button' ) . replaceWith ( $compile ( scope . options . submitButtonTemplate ) ( scope ) ) ;
93
83
}
94
- } ) ;
95
- } , true ) ;
96
- }
97
- ]
84
+ //Post gets called after angular has created the FormController
85
+ //Now pass the FormController back up to the parent scope
86
+ scope . formOnParentScope = scope [ attr . name ] ;
87
+ }
88
+ } ;
89
+ } ,
90
+ controller : [
91
+ '$scope' ,
92
+ '$element' ,
93
+ '$parse' ,
94
+ function ( $scope , $element , $parse ) {
95
+ $scope . $watch ( 'result' , function ( newValue ) {
96
+ angular . forEach ( $scope . fields , function ( field , index ) {
97
+ if ( field . hideExpression ) {
98
+ var getter = $parse ( field . hideExpression ) ;
99
+ field . hide = getter ( $scope . result ) ;
100
+ }
101
+ } ) ;
102
+ } , true ) ;
103
+ }
104
+ ]
105
+ } ;
106
+ }
107
+ ] ) ;
108
+ 'use strict' ;
109
+ angular . module ( 'formly.render' ) . provider ( 'formlyOptions' , function ( ) {
110
+ var options = {
111
+ uniqueFormId : null ,
112
+ submitCopy : 'Submit' ,
113
+ hideSubmit : false ,
114
+ submitButtonTemplate : null
115
+ } ;
116
+ function setOption ( name , value ) {
117
+ if ( typeof name === 'string' ) {
118
+ options [ name ] = value ;
119
+ } else {
120
+ angular . forEach ( name , function ( val , name ) {
121
+ setOption ( name , val ) ;
122
+ } ) ;
123
+ }
124
+ }
125
+ function getOptions ( ) {
126
+ // copy to avoid third-parties manipulating the options outside of the api.
127
+ return angular . copy ( options ) ;
128
+ }
129
+ this . setOption = setOption ;
130
+ this . getOptions = getOptions ;
131
+ this . $get = function formlyOptions ( ) {
132
+ return this ;
98
133
} ;
99
134
} ) ;
100
135
'use strict' ;
@@ -143,6 +178,6 @@ angular.module('formly.render').run([
143
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>' ) ;
144
179
$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>' ) ;
145
180
$templateCache . put ( 'directives/formly-field.html' , '' ) ;
146
- $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 ng-hide=options.hideSubmit>{{options.submitCopy || "Submit"}}</button></form>' ) ;
181
+ $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>' ) ;
147
182
}
148
183
] ) ;
0 commit comments