将类添加到transcluded元素

时间:2015-04-22 20:17:02

标签: angularjs

假设我的指令看起来像这样:

delete

我想最终得到这个:

<input class="icon-input" type="text">

(上面的重要部分是<div class="col-xs-12"> <i ng-class="icon"></i> <input class="icon-input" type="text" class="form-control"> </div> 。)

我的指令目前看起来像这样:

class="form-control"

如何将'use strict'; class IconInput { constructor() { this.restrict = 'C'; this.replace = true; this.transclude = 'element'; this.templateUrl = 'app/form-fields/icon-input/icon-input.html'; this.scope = { icon: '@' } } } export default IconInput; 添加到已转置的class="form-control"元素中?

1 个答案:

答案 0 :(得分:2)

我最终自己搞清楚了。

'use strict';

class IconInput {
  constructor() {
    this.restrict = 'C';
    this.transclude = true;
    this.scope = { icon: '@' };

    this.compile = (element, attrs, transclude) => {
      return $scope => {
        transclude($scope, clone => {
          element.append(clone)
            .wrap('<div class="col-xs-12"></div>')
            .before(`<i class="fa ${$scope.icon}"></i>`)
            .addClass('form-control');
        });
      };
    };
  }
}

export default IconInput;