如何使用引导程序在选择框上实现 onChange 函数

use*_*240 5 select onchange twitter-bootstrap angular-formly

在阅读了很多帖子后,我决定这应该有效:

vm.model.myChange = function(element) {
  console.log(element);
}
Run Code Online (Sandbox Code Playgroud)

.. 在 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"
  }]
}
Run Code Online (Sandbox Code Playgroud)

在实践中这没有效果,即我的函数没有被调用。生成的表单也不包含对我的函数的引用。

我找不到如何执行此操作的工作示例。有什么建议?

谢谢。保罗

Mat*_*ttE 5

您可以执行以下三件事中的任何一项:

templateOptions: {
   onChange: function($viewValue, $modelValue, $scope) {
   //implement logic here
   };
}
Run Code Online (Sandbox Code Playgroud)

或者

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

或者

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


CDa*_*gua 3

我处理这个问题的一种方法是让我的 json 字段由服务提供。有了这个,就可以轻松地调用函数。

注意:这是由服务或工厂提供的

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"
           }]
       }
   ] 
};
Run Code Online (Sandbox Code Playgroud)