FormItem.js 1.28 KB
/**
 * 表单域
 */
import template from "./FormItem.html";
angular.module("esNgAntd").directive("antdFormItem", function () {
  return {
    template: template,
    restrict: "E",
    replace: true,
    transclude: true,
    scope: {
      name: "@",
      label: "@",
      labelCol: "=",
      wrapperCol: "=",
      required: "="
    },
    require: ["^?antdForm"],
    controller: function ($scope, $element, $attrs) {
      this.getContext = function () {
        return $scope;
      };

      $scope.state = {
        labelCol: null,
        wrapperCol: null
      };
    },
    link: function ($scope, $element, $attrs, $controllers) {
      let [antdForm] = $controllers;

      if (antdForm) {
        $scope.antdForm = antdForm.getContext();
      }

      if ($scope.labelCol && $scope.labelCol.span) {
        $scope.state.labelCol = $scope.labelCol.span;
      } else if ($scope.antdForm.labelCol && $scope.antdForm.labelCol.span) {
        $scope.state.labelCol = $scope.antdForm.labelCol.span;
      }

      if ($scope.wrapperCol && $scope.wrapperCol.span) {
        $scope.state.wrapperCol = $scope.wrapperCol.span;
      } else if ($scope.antdForm.wrapperCol && $scope.antdForm.wrapperCol.span) {
        $scope.state.wrapperCol = $scope.antdForm.wrapperCol.span;
      }
    }
  };
});