How to implement an onChange function on a select box in formly with bootstrap

user1712240 picture user1712240 · Jun 26, 2015 · Viewed 8k times · Source

After reading quite a few posts I decided that this ought to work:

vm.model.myChange = function(element) {
  console.log(element);
}

.. and in the vm.fields:

{
"key": "transportation",
"type": "select",
"templateOptions": {
  "label": "How do you get around in the city",
  "valueProp": "name",
  "onChange": "model.myChange()", // ADDED
  "options": [{
    "name": "Car"
  }, {
    "name": "Helicopter"
  }]
}

In practice this has no effect, i.e. my function is not called. The generated form contained no reference to my function either.

I could not find a working example of how to do this. Any suggestions?

Thx. Paul

Answer

MattE picture MattE · Dec 14, 2016

you can do any of these three things:

templateOptions: {
   onChange: function($viewValue, $modelValue, $scope) {
   //implement logic here
   };
}

OR

templateOptions: {
   onChange: callMe()
},
controller: function($viewValue, $modelValue, $scope) {
    $scope.callMe = function() {
    //implement logic here
    };
}

OR

    templateOptions: {
       onChange: ''
    },
    expressionProperties : {
       'templateOptions.onChange': function($viewValue, $modelValue, $scope) {
       //implement logic here
       };
   }