问题
I'm new to formly, I want to change the templateOptions property based on value, so below is my config
formlyConfig.setType({
name: 'phone',
extends: 'maskedInput',
defaultOptions: {
templateOptions: {
mask: '999-9999-9999'
},
I want to change the mask to mask: '999-999-9999' on a button click,
I tried to do this on onclick of button , but no change
formlyConfig.setType({
name: 'phone',
defaultOptions:{
templateOptions: {
mask: '999-999-9999'
},
}
})
;
回答1:
I'm considering you're using angular-formly. Here I don't think you can change config options on the go. So, to do that you've to delete the field from DOM & then reload it with new config option (e.g. mask). So, you can call method on click of any button, in which you can reset some boolean flag & then update config option & then set the flag.
<button class="btn" ng-click="vm.clickFn()">click</button>
Where clickFn can be:
vm.clickFn = function() {
vm.show = false;
vm.fields[0].templateOptions.mask = "999-999-9999";
$timeout(function() {
vm.show = true;
});
}
Here timemout is just a small time to let it update the flag & run digest cycle.
Example to refer.
来源:https://stackoverflow.com/questions/52849809/formly-update-templateoptions-on-click-of-button