I'm attempting to set up some validation in Angular Formly forms. Below is a snippet of my code. When I run the app, I get an unknown provider error .
var genericFormApp = angular.module('genericFormApp', ['formly', 'formlyMaterial', 'ngRoute', 'ngAnimate', 'ngAria', 'ngMaterial', 'ngMessages', 'angular.filter', 'ngSanitize', 'angularLazyImg', 'angular-slideout']);
var conf = {
validation: {
stringMessages: {
required: "Required.",
email: "Must be a valid email address.",
// generic
valid: "Must be a valid value.",
},
toMessages: [
{
name: "valid",
prop: "datepicker",
prefix: "",
postfix: "",
defaultMessage: "Must be a valid date."
}
],
}
};
genericFormApp.constant("conf", conf);
genericFormApp.run(function (formlyConfig, formlyValidationMessages, conf) {
// set types here
var i, k, str = conf.validation.stringMessages, to, toMess = conf.validation.toMessages;
// generic messages
for (i in str) {
formlyValidationMessages.addStringMessage(i, str[i]);
}
// template option messages, to optionally override
for (i in toMess) {
to = toMess[i];
formlyValidationMessages.addTemplateOptionValueMessage(to.name, to.prop, to.prefix, to.postfix, to.defaultMessage);
}
});
If I change the run function to this: genericFormApp.run(function (formlyConfig, formlyValidationMessages, conf) {}
the app runs.
Now if I try to access conf, I get the unknown provider error: genericFormApp.run(function (formlyConfig, formlyValidationMessages, conf) {console.log(conf)}
Try this, is working
genericFormApp.constant('conf', (function () {
var conf = {
validation: {
stringMessages: {
required: "Required.",
email: "Must be a valid email address.",
// generic
valid: "Must be a valid value."
},
toMessages: [
{
name: "valid",
prop: "datepicker",
prefix: "",
postfix: "",
defaultMessage: "Must be a valid date."
}
]
}
};
return conf;
})())
My problem was that (as I'm using Visual Studio), the minification that occurs, causes the run function to no run. I had to inject the names:
genericFormApp.run(['formlyConfig', 'formlyValidationMessages', 'conf', function(formlyConfig, formlyValidationMessages, conf) {
// set types here
var i, k, str = conf.validation.stringMessages, to, toMess = conf.validation.toMessages;
// generic messages
for (i in str) {
formlyValidationMessages.addStringMessage(i, str[i]);
}
// template option messages, to optionally override
for (i in toMess) {
to = toMess[i];
formlyValidationMessages.addTemplateOptionValueMessage(to.name, to.prop, to.prefix, to.postfix, to.defaultMessage);
}
}]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.