【发布时间】:2018-02-08 15:44:51
【问题描述】:
我已经能够升级 angularjs 元素指令以在 angular 4 中使用。 这是一个示例代码:
[myScores.js]
angular.module('app.components.directives.myScores', [])
.directive('myScores', function() {
return {
scope: {
score: '=',
},
template: '<div>>>> Your score is {{score}} <<<',
link: function(scope) {
console.log("in myScores", scope)
}
};
});
[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);
}
}
请注意,我正在使用 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');
}
};
});
[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);
}
}
加载具有以下内容的页面时出现错误:
<div makeGreen>Text should be green</div>
我收到了这个错误:
Error: Directive 'makeGreen' is not a component, it is missing template.
【问题讨论】:
-
你搞定了吗?
-
@jamiebarrow 我很久以前就放弃了。
-
好吧,很遗憾,我想在 Angular 中重写属性很容易,但是是的......如果我还不需要它会很好:) 无论如何谢谢回到我身边
-
@jamiebarrow 我找到了一种在 Angular 2(现在是 5)中公开 AngularJS 指令的方法。编写一个 Angular JS 组件来使用 Angular JS 指令并在 Angular 2 中升级该组件。这意味着您必须运行 AngularJS 1.5 或更高版本。
-
@pateketu 截至 2017 年 8 月,我没有找到将属性指令迁移到 Angular 4 的方法。这是不可能的。不确定 Angular 团队是否让这成为可能。不过我不会屏住呼吸。
标签: angularjs attributes upgrade directive