【问题标题】:Angular formly - Checkbox model values templateAngular formly - 复选框模型值模板
【发布时间】:2017-03-24 01:55:02
【问题描述】:

我有一个小表单,它具有某些复选框的默认值,例如,有一个人模型,当单击 formly 生成的表单中的复选框“狗”时,它将添加到“宠物”数组中的字符串构成人物模型一部分的狗。我怎样才能通过正式的方式实现这一目标?

{
     key: "dog",
     type: "checkbox",
     templateOptions: {
          ???
     }
}

表格示例:

Person name: Joe
Pets:
[x] dog

这会将模型设置为:

 {
      "name": "Joe"
      "pets": [ "dog" ]
 }

【问题讨论】:

  • 请提供有关问题的更多详细信息。
  • 添加了更多信息,希望对您有所帮助。
  • 类似的问题在那个帖子中得到了解决:stackoverflow.com/q/14514461/2275775 答案很长,很受欢迎,而且经过充分检验,所以创建新的不是最好的主意。

标签: javascript angularjs angular-formly


【解决方案1】:

@veg,这个问题是关于角度形式的,而不是角度的。这不一样。很明显。

在你的初始化中:

vm.model = { //initialize a model with a pets property holding an empty array.
  pets: []
}

在您的现场模型中:

{
   key: 'dog',
   type: 'checkbox',
   templateOptions: {
      label: 'dog',
      onChange: function($viewValue, $modelValue, $scope) {
        if($modelValue) $scope.model.pets.push('dog'); //if it's true--add it
        else { //need to remove it from the array
           var index = $scope.model.pets.indexOf('dog'); //get the index
           $scope.model.pets.slice(index, index+1); //remove it from the array
        }
      }
   },
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 2020-09-24
    • 1970-01-01
    相关资源
    最近更新 更多