以角度形式包裹div中的一些字段

时间:2016-07-05 09:35:47

标签: angularjs angular-formly

如何在div中包含一些字段而不使用fieldGroup。我希望所有div都有类'left-layout'将被类'left-container'包裹 控制器:

vm.fields = [
      {
        key: 'first',
        type: 'input',
        className: 'left-layout',
        model: vm.model.name,
        templateOptions: {
          label: 'First Name'
        }
      },
      {
        key: 'last',
        type: 'input',
        model: vm.model.name,
        className: 'left-layout',
        templateOptions: {
          label: 'Last Name'
        }
      },
      {
        key: 'email',
        type: 'input',
        templateOptions: {
          label: 'Email Address',
          type: 'email'
        }
      }
];

的index.html

<script>
      $('.left-layout').wrapAll("<div class='left-container'></div>");
</script>

这是jsbin链接:http://jsbin.com/honuxi/edit?html,js,output

1 个答案:

答案 0 :(得分:0)

为什么不想使用fieldGroup?这就是你如何做到的。您将className添加到fieldGroup,然后使用您在formly config中预定义的包装器包装fieldGroup:

   .config(function config(formlyConfigProvider) {
                formlyConfigProvider.setWrapper({
                    name: 'left-container',
                    template: '<div class="left-container">  
                                 <formly-transclude></formly-transclude>  
                              </div>'                                                                                                      
                });
    }


vm.fields = [
   {
     wrapper: 'left-container',
     className: 'left-layout',
     fieldGroup: [
      {
        key: 'first',
        type: 'input',
        model: vm.model.name,
        templateOptions: {
          label: 'First Name'
        }
      },
      {
        key: 'last',
        type: 'input',
        model: vm.model.name,
        templateOptions: {
          label: 'Last Name'
        }
      },
      {
        key: 'email',
        type: 'input',
        templateOptions: {
          label: 'Email Address',
          type: 'email'
        }
      }
   }]
];