How to implement the onChange function in the selection window formally using bootstrap

After reading a few posts, I decided that this should work:

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

.. and in 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 does not work, i.e. my function is not being called. The generated form does not contain links to my function.

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

thank. Floor

+4
source share
2 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
       };
   }
+2
source

One of the ways I dealt with is using my json fields served by the service. With this you can conveniently call the function.

NB: this is served by a service or factory

var getFields = function () {
    var fields = [
        {
           "key": "transportation",
           "type": "select",
           "templateOptions": {
           "label": "How do you get around in the city",
           "valueProp": "name",
           "onChange": "function(){//Implement your logic}"
           "options": [{
                "name": "Car"
           }, {
             "name": "Helicopter"
           }]
       }
   ] 
};
+1
source

All Articles