从渲染器迁移到渲染器2
migrate from renderer to renderer2
我将一个项目从 Angular 7 迁移到 8,我不得不将渲染器 class 更改为渲染器 2,我遇到了以下错误:
ERROR Error: Template error: Can't bind to 'disabled' since it isn't a known property of 'div'.
at createUnknownPropertyError (vendor.js:83083)
at validateAgainstUnknownProperties (core.js:12699)
at elementPropertyInternal (core.js:12604)
...
渲染器的 class 表现如何:
import { OnInit, Directive, Input, Renderer, ElementRef } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Response } from '@angular/http';
import { Loader } from './loader';
@Directive({
selector: '[asCardLoader]'
})
export class CardDirective implements OnInit {
private nativeElement: Node;
private loaderElement: Node;
constructor(private renderer: Renderer, private element: ElementRef) {
this.nativeElement = element.nativeElement;
}
ngOnInit() {
if (this.loader) {
this.initLoader();
}
showLoading() {
const divElement = this.renderer.createElement(this.nativeElement, 'div');
this.renderer.setElementClass(divElement, 'card-loading', true);
this.renderer.setElementStyle(divElement, 'display', 'block');
this.loaderElement = divElement;
const nextElem = this.element.nativeElement.nextElementSibling;
const parent = this.element.nativeElement.parentNode;
parent.insertBefore(divElement, nextElem);
// other methods
我对 renderer2 的尝试
import { OnInit, Directive, Input, Renderer2, ElementRef } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Response } from '@angular/http';
import { Loader } from './loader';
@Directive({
selector: '[asCardLoader]'
})
export class CardLoaderDirective implements OnInit {
private nativeElement: Node;
private loaderElement: Node;
@Input()
private loader: Loader;
constructor(private renderer: Renderer2, private element: ElementRef) {
this.nativeElement = element.nativeElement;
}
ngOnInit() {
if (this.loader) {
this.initLoader();
}
}
showLoading() {
const divElement = this.renderer.createElement('div');
this.renderer.appendChild(this.nativeElement, divElement);
this.renderer.addClass(divElement, 'card-loading');
this.renderer.setStyle(divElement, 'display', 'block');
this.loaderElement = divElement;
const nextElem = this.element.nativeElement.nextElementSibling;
const parent = this.element.nativeElement.parentNode;
parent.insertBefore(divElement, nextElem);
}
对于 renderer2,creatElement 方法尽管具有相同的名称,但不再以相同的方式工作,而且我从文档中不能很好地理解应该进行此转换的正确方法。
NativeElement (div) 我认为它已禁用 属性,因为我在您的示例中没有看到任何禁用 属性、
的地方
该元素没有禁用 属性。
如果你需要添加 disabled 没有错误,你可以使用下面的例子
<div [attr.disabled]="condition ? true : null">
我将一个项目从 Angular 7 迁移到 8,我不得不将渲染器 class 更改为渲染器 2,我遇到了以下错误:
ERROR Error: Template error: Can't bind to 'disabled' since it isn't a known property of 'div'.
at createUnknownPropertyError (vendor.js:83083)
at validateAgainstUnknownProperties (core.js:12699)
at elementPropertyInternal (core.js:12604)
...
渲染器的 class 表现如何:
import { OnInit, Directive, Input, Renderer, ElementRef } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Response } from '@angular/http';
import { Loader } from './loader';
@Directive({
selector: '[asCardLoader]'
})
export class CardDirective implements OnInit {
private nativeElement: Node;
private loaderElement: Node;
constructor(private renderer: Renderer, private element: ElementRef) {
this.nativeElement = element.nativeElement;
}
ngOnInit() {
if (this.loader) {
this.initLoader();
}
showLoading() {
const divElement = this.renderer.createElement(this.nativeElement, 'div');
this.renderer.setElementClass(divElement, 'card-loading', true);
this.renderer.setElementStyle(divElement, 'display', 'block');
this.loaderElement = divElement;
const nextElem = this.element.nativeElement.nextElementSibling;
const parent = this.element.nativeElement.parentNode;
parent.insertBefore(divElement, nextElem);
// other methods
我对 renderer2 的尝试
import { OnInit, Directive, Input, Renderer2, ElementRef } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Response } from '@angular/http';
import { Loader } from './loader';
@Directive({
selector: '[asCardLoader]'
})
export class CardLoaderDirective implements OnInit {
private nativeElement: Node;
private loaderElement: Node;
@Input()
private loader: Loader;
constructor(private renderer: Renderer2, private element: ElementRef) {
this.nativeElement = element.nativeElement;
}
ngOnInit() {
if (this.loader) {
this.initLoader();
}
}
showLoading() {
const divElement = this.renderer.createElement('div');
this.renderer.appendChild(this.nativeElement, divElement);
this.renderer.addClass(divElement, 'card-loading');
this.renderer.setStyle(divElement, 'display', 'block');
this.loaderElement = divElement;
const nextElem = this.element.nativeElement.nextElementSibling;
const parent = this.element.nativeElement.parentNode;
parent.insertBefore(divElement, nextElem);
}
对于 renderer2,creatElement 方法尽管具有相同的名称,但不再以相同的方式工作,而且我从文档中不能很好地理解应该进行此转换的正确方法。
NativeElement (div) 我认为它已禁用 属性,因为我在您的示例中没有看到任何禁用 属性、
的地方该元素没有禁用 属性。 如果你需要添加 disabled 没有错误,你可以使用下面的例子
<div [attr.disabled]="condition ? true : null">