是否可以升级angularjs atttribute指令以在角度4中使用?

Wil*_*ill 8 attributes directive upgrade angularjs

我已经能够升级angularjs元素指令以在角度4中使用.这是一个示例代码:

[myScores.js]

angular.module('app.components.directives.myScores', [])
.directive('myScores', function() {
  return {
    scope: {
      score: '=',
    },
    template: '<div>&gt;&gt;&gt; Your score is {{score}} &lt;&lt;&lt;',
    link: function(scope) {
      console.log("in myScores", scope)
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

[myScores.ts]

import { Directive, ElementRef, Injector, Input, Output, EventEmitter } from '@angular/core';
import { UpgradeComponent } from '@angular/upgrade/static';

@Directive({
  selector: 'my-scores'
})
export class MyScoresDirective extends UpgradeComponent {
  @Input() score: number;

  constructor(elementRef: ElementRef, injector: Injector) {
    super('myScores', elementRef, injector);
  }
}
Run Code Online (Sandbox Code Playgroud)

注意我正在使用UpgradeComponent来升级myScores元素指令.我在属性指令上尝试过相同但是出错了.有没有办法升级属性指令?

这是我尝试升级属性指令:

[makeGreen.js]

angular.module('app.components.directives.makeGreen', [])
.directive('makeGreen', function() {
  return {
    restrict: 'A',
    link: function(scope, element) {
      console.log("in makeGreen", scope)
      console.log("element", element)
      element.css('color', 'green');
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

[makeGreen.ts]

import { Directive, ElementRef, Injector, Input, Output, EventEmitter } from '@angular/core';
import { UpgradeComponent } from '@angular/upgrade/static';

@Directive({
  selector: '[makeGreen]'
})
export class MakeGreenDirective extends UpgradeComponent {
  @Input() count: number;
  @Output() clicked: EventEmitter<number>;

  constructor(elementRef: ElementRef, injector: Injector) {
    console.log("elementRef", elementRef.nativeElement)
    super('makeGreen', elementRef, injector);
  }
}
Run Code Online (Sandbox Code Playgroud)

加载具有以下内容的页面时出错:

<div makeGreen>Text should be green</div>
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误:

Error: Directive 'makeGreen' is not a component, it is missing template.
Run Code Online (Sandbox Code Playgroud)

nir*_*aft 0

属性指令完全可以具有输入属性,该属性可以从使用它的标签传递给它:例如:

<p highlightThis [highlightColor]="'orange'" [count]="5">Highlighted in orange</p>
Run Code Online (Sandbox Code Playgroud)

还要确保您的 appModule/SharedModule 导入它。

所以我看到的问题是你定义结构指令的方式。结构指令不附加任何模板结构。它适用于任何使用它的 html 标签。

对于您的情况,我看到您正在使用 Component 类扩展指令,这对于Attribute 指令来说是不可接受的:--

MakeGreenDirective extends UpgradeComponent {
Run Code Online (Sandbox Code Playgroud)

您应该将属性指令与任何组件分开。例如:

  import { Directive, ElementRef, Injector, Input, Output, EventEmitter } from 
  '@angular/core';

  @Directive({
    selector: '[highlightThis]'
   })

 export class HighLightThisDirective {
 @Input() count: number;
 @Input() highlightColor: string;
 @Output() clicked: EventEmitter<number>;

 constructor(private elementRef: ElementRef, injector: Injector) {  }

 ngOnInit(): void {
   this.decorateMyTag();
 }

 private decorateMyTag(): void {
   console.log("highlightColor", this.highlightColor);
   this.elementRef.nativeElement.style.backgroundColor = this.highlightColor;
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这并不能回答问题。您正在提供一个 Angular 解决方案,其中问题询问如何升级(即使用“@angular/upgrade/static”)AngularJS 指令。 (5认同)