简体   繁体   English

在ES6 + Angular 1.4指令中插值

[英]Interpolate in ES6 + Angular 1.4 directive

I am currently trying the 'new' ES6 + Angular combination and got stuck on interpolating a html string in a directive that contains scope bindings. 我目前正在尝试使用“新” ES6 + Angular组合,并陷入了在包含范围绑定的指令中插入html字符串的难题。

I have tried the following option: 我尝试了以下选项:

Current situation 现在的情况

The following code works but it uses a filter instead of a directive. 以下代码可以使用,但是它使用过滤器而不是指令。

HTML file HTML文件

 <div class="thumbnail-body">
     <div ng-bind-html="vm.labels.hello | interpolate:this"></div>
 </div>

filter in module (still old school angular without ES6) 模块中的过滤器(不带ES6的老式过滤器)

//TODO: .filter('interpolate', () => new InterpolateFilter())
.filter('interpolate', function ($interpolate) {
  return function (text, scope) {
    if (text) {
      return $interpolate(text)(scope);
    }
  };
});

The reason why I am trying to move the interpolate logic towards a directive so I don't have to add a filter on multiple elements. 我之所以尝试将插值逻辑移向指令,是因为不必在多个元素上添加过滤器。

working but ugly situation 工作但丑陋的情况

HTML file HTML文件

<interpolate value="vm.labels.hello"  scope="this"></interpolate>

Directive 指示

class InterpolateDirective {
  constructor() {    
   this.template = '<div ng-bind-html="value |interpolate:scope"></div>';
   this.restrict = 'E';
   this.scope = {value: '=',scope:'='};
  }
}
export default InterpolateDirective;

Module

.directive('interpolate', () => new InterpolateDirective())

Desired situation (not working yet) 所需的情况(尚不可用)

HTML file HTML文件

<interpolate value="vm.labels.hello"></interpolate>

Directive 指示

class InterpolateDirective {
      constructor($interpolate,$scope) {
      'ngInject';this.template = '<div ng-bind-html="value"> </div>';
      this.restrict = 'E';
      this.scope = {value: '='};
      this.$interpolate = $interpolate;
      this.$scope = $scope;
  }
  //optional compile or link function
  link() {
     this.scope.value = this.$interpolate(this.scope.value)(this);
  }
}
export default InterpolateDirective;

Module

.directive('interpolate', () => new InterpolateDirective())

In short: I would like to work with the desired situation 简而言之:我想在理想的情况下工作

Try this: 尝试这个:

class InterpolateDirective {
    constructor($interpolate) {
        this.template = '<div ng-bind-html="value"> </div>';
        this.restrict = 'E';
        this.scope = {
            value: '='
        };
        this.$interpolate = $interpolate;

        InterpolateDirective.prototype.link = (scope) =>{
            scope.value = this.$interpolate(scope.value)(this);
        }
    }

    public static Factory() {
        var directive = ($interpolate) => {
            return new InterpolateDirective($interpolate);
        };
        directive['$inject'] = ['$interpolate'];
        return directive;
    }
}
export default InterpolateDirective;


.directive('interpolate', InterpolateDirective.Factory());

Scope in directives isn't injected like in controllers by dependency injection. 指令中的作用域不会像依赖注入中的控制器那样注入。 Directive can access scope by first parameter of link function. 指令可以通过链接功能的第一个参数访问范围。

Scope defined by directive's object property isn't the same. 指令的对象属性定义的范围不相同。 It's a part of configuration to create directive's API by scope isolation. 通过范围隔离创建指令的API是配置的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM