0

我将一个项目从 Angular 7 迁移到 8,我不得不将 Renderer 类更改为 Renderer2,我面临以下错误:

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)
    ...

渲染器的课程如何:


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 方法尽管名称相同,但不再以相同的方式工作,而且我从文档中不太了解应该进行此转换的正确方式。

4

1 回答 1

0

NativeElement (div) 我认为它具有禁用属性,因为我在您的示例禁用属性中看不到任何位置,

该元素没有 disabled 属性。如果您需要添加禁用且没有错误,您可以使用该示例

<div [attr.disabled]="condition ? true : null">

于 2020-09-26T21:35:00.697 回答