angular-formly - 角度形式为 : calculate value of one field based on other fields input

标签 angular-formly

json 配置:

{
    "moduleconfigs": {
      "create": [
        {
          "key": "Committed",
          "type": "horizontalInput",
          "templateOptions": {
            "label": "Committed"
          }
        },
        {
          "key": "Uncommitted",
          "type": "horizontalInput",
          "templateOptions": {
            "label": "Uncommitted"
          }
        },
        {
          "key": "Line",
          "type": "horizontalInput",
          "templateOptions": {
            "label": "Line"
          }
        },{
          "key": "Total",
          "type": "horizontalInput",
          "templateOptions": {
            "label": "Total"
          },
          "expressionProperties":{
            "value": function($viewValue, $modelValue, scope){
                return scope.model.lineFill+scope.model.uncommitedBPD+scope.model.commitedBPD;
            }
          }
        }
        ]
    }
}

html:

 <form>
     <formly-for model="vm.myModel" fields="vm.myFields"></formly-form> </form>

我是 Angular 的新手。我正在使用 Angular Formly json 创建表单。总计字段应显示“已提交”+“未提交”+“行”字段中提供的值的总和。我正在使用 expressionProperties 但不起作用。

最佳答案

我猜您已经解决了这个问题...但是...

你做错了两件事。

第一(1):它们在正式字段配置对象中的键值是在模型上按该名称设置值。

所以你的第一个字段配置对象是:

{
  "key": "Committed",
  "type": "horizontalInput",
  "templateOptions": {
    "label": "Committed"
  }
},

然后,您尝试使用键 commitedBPD 访问该值,因此您将始终无法定义。

基本上形式是使用 Comlated 键设置模型对象上该字段中的输入值,您需要更改键以匹配。

第二 (2):我可能是错的,但我认为您不能使用表达式属性来设置这样的值。 Formly 会自动尊重模型上的值更改,因此您最好在其他 Formly 字段配置对象上放置 onChange 来进行解析和添加,如下所示:

{
  "key": "Committed",
  "type": "horizontalInput",
  "templateOptions": {
  "label": "Committed"
    "onChange": addTotal
  }
}...


function addTotal() {
  //You have access to the model here because it's in your controller
  // NOTE: the parseInput function you'll have to write yourself
  vm.model.Total = parseInput(vm.model.Committed) + ...
}

总而言之,您最大的问题是尝试使用错误的键访问模型对象中的值

关于angular-formly - 角度形式为 : calculate value of one field based on other fields input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33823424/

相关文章:

javascript - 自定义 DatePicker Angular Formly 字段不会出现验证错误消息

angularjs - 如何以 Angular 形式更改 Angular UI Datepicker 模型值格式?

Angular 5,请告诉我 FormlyFieldConfig 中 ngx 支持的类型列表

angularjs - 在 Angular 中编写自定义控件或小部件

javascript - angularjs 在 safari 和 IE 11 中正式提交错误

angularjs - 动态加载形式预输入选项

node.js - 无法正式安装 "Cannot read property ' 种'未定义'

angularjs - 如何在 Angular 中正式使用 css 类

javascript - Angularjs 正式嵌套形式

angularjs - 如何以 Angular 形式将数据类型添加为日期?