Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 307
user1712240 Avatar asked Sep 27 '22 12:09

user1712240


1 Answers

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
       };
   }
like image 96
MattE Avatar answered Oct 02 '22 15:10

MattE