【问题标题】:How to implement an onChange function on a select box in formly with bootstrap如何使用引导程序在选择框上正式实现 onChange 函数
【发布时间】:2015-06-26 07:49:43
【问题描述】:

在阅读了很多帖子后,我认为这应该可行:

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

.. 在 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"
  }]
}

实际上这没有任何效果,即我的函数没有被调用。生成的表单也不包含对我的函数的引用。

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

谢谢。保罗

【问题讨论】:

标签: twitter-bootstrap select onchange angular-formly


【解决方案1】:

你可以做以下三件事中的任何一件:

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

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

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

【讨论】:

  • 这(第一个选项)对我没有任何帮助;我是正式的新手,但我想它与碰巧使用的任何模板中可用的特定templateOptions 有关?使用change 而不是onChange,如此处所示stackoverflow.com/a/59791792/2442167 对我有用。
【解决方案2】:

我处理这个问题的一种方法是让我的 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"
           }]
       }
   ] 
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多