Formly update templateOptions on click of button

烈酒焚心 提交于 2021-01-29 14:39:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!