Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model binding addonLeft.text angular formly

I am using bootstrap templates with angular-formly and I wanted to bind addonsLeft.text to a model so that it dynamically changes once a select option has changed.

This is how an input looks like:

  {
    key: 'my_input',
    type: 'input',
    templateOptions: {
      label: 'Text',
      addonLeft: {
            text: vm.model.select
      }
    }
  }

From what I understood bootstrap templates do not create a model for text / class option and therefore changing the model wouldn't affect the addonLeft.text.

Find a non-working example in this jsbin link.

I have found a hacky way of doing this butI am not sure if this is the right way.

Here is the solution in this jsbin link.


Should I create a custom template instead so that it has a proper model etc? Reason I haven't done this already it's because there is already an implemented solution that would keep my code cleaner.

like image 385
George Nikolaides Avatar asked May 27 '26 10:05

George Nikolaides


1 Answers

This is a perfect case for Formly Expressions:

{
    key: 'my_input',
    type: 'input',
    templateOptions: {
        label: 'Text',
        addonLeft: {
            text: ''
        }
    },
    expressionProperties: {
      'templateOptions.addonLeft.text': 'model.select' // <- HERE
    }
}

Example JSBin: http://jsbin.com/tudivi/1/edit?html,js,console,output

like image 176
Ben Orozco Avatar answered May 30 '26 03:05

Ben Orozco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!