【问题标题】:How to add dynamic class for formly-field based on controller property?如何根据控制器属性为表单字段添加动态类?
【发布时间】:2017-08-31 22:48:16
【问题描述】:

我需要根据控件属性在表单字段中添加条件类。

例如:我的控制器中有

 $scope.isIssueFixed = true; 

我的正式对象是 -

$scope.formly = { 类名:'col-xs-6', 键:'名称', 类型:'输入', 模板选项:{ 标签:'通用设备名称' }

现在我们如何在上面的表单字段中添加一个条件类, $scope.isIssueFixed?'ok':'No';正式反对?

【问题讨论】:

    标签: javascript angularjs angular-formly


    【解决方案1】:

    我以角度形式使用 Wrappers 解决了这个问题。这是我所做的代码。可能对某人有帮助。

    将以下代码添加到配置中:

    formlyConfigProvider.setWrapper({
          name: 'horizontalBootstrapLabel',
          template: [
            `<label for="{{::id}}" class="col-sm-2 control-label" >
              '{{to.label}} {{to.required ? "*" : ""}}
            </label>
            <div class="col-sm-8" ng-class="{'bbb':model.heightlight}">
              <formly-transclude></formly-transclude>
            </div>`
          ].join(' ')
    });
    

    【讨论】:

      【解决方案2】:
       $scope.$watch('isIssueFixed', function() {
              var classAdded = $scope.isIssueFixed ? "ok" , "No";  $scope.formly = {
                       className: 'col-xs-6 ' + classAdded  ,
                       key: 'products.device[0].deviceCommonName',
                       type: 'input',
                       templateOptions: {
                       label: 'Common Device Name'
                      }
          });
      

      【讨论】:

      • 上述解决方案不起作用,我需要根据条件动态添加类,如果控制属性发生变化,上述解决方案类将不会更新。谢谢回答。每当 scope.IsIssuefixed 发生变化时,我都会更新正式字段中的类
      • 更新了答案现在试试 1
      【解决方案3】:

      您可以使用 angularjs 的 ng-class 指令。参考https://docs.angularjs.org/api/ng/directive/ngClass

      【讨论】:

      • 嘿 kaushlendras,感谢您的快速响应,但我想使用 ExpressionProperties 在模板选项中动态更改类?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多