gpt4 book ai didi

angular - 无效字符错误 Angular

转载 作者:太空狗 更新时间:2023-10-29 19:28:41 24 4
gpt4 key购买 nike

我得到 ERROR DOMException [InvalidCharacterError: "String contains an invalid character"
代码:5
nsresult: 0x80530005
location: http://localhost:4200/vendor.bundle.js:67608]
尝试使用自定义属性指令编译 Angular 组件时。我的 AppComponent、指令和基本模板的代码如下:

leftAlign.directive.ts

import {
Directive,
HostListener,
HostBinding,
Input,
Output,
ElementRef,
Renderer2 // for host class binding
} from '@angular/core';

@Directive({
selector: '[leftAlign]'
})
export class LeftAlignDirective {

public constructor(
private el: ElementRef,
private renderer: Renderer2
) {
this.renderer.addClass(this.el.nativeElement, 'ui leftAlign');
}
}

应用程序组件.ts

import { Component } from '@angular/core';
import { LeftAlignDirective } from './Directives/left-align.directive';


@Component({
selector: `app-root`,
templateUrl: './app.component.html'
})
export class AppComponent {
public static SemanticUiBaseDir = '../../semantic/dist/';
public getSemanticeUiBaseDir() : string{
return AppComponent.SemanticUiBaseDir;
}
public constructor() {}
}

app.component.html

!--
@ semantic-ui.min.css
-->
<link rel="stylesheet" type="text/css" href="/home/zerocool/km-live/FrontEndComponent/semantic/dist/semantic.css">

<!--
@ @semantic.min.js
-->
<script src="./home/zerocool/km-live/FrontEndComponent/semantic/semantic.js"></script>

<p leftAlign>This should be aligned left!</p>

我有兴趣了解以下内容:1. 是什么导致了这样的错误?2、本例错误是什么原因造成的?

谢谢

最佳答案

问题是addClass中的空格,不允许。

当我尝试这样做时,我得到了一个更具描述性的错误

ERROR DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('ui leftAlign') contains HTML space characters, which are not valid in tokens.

您需要单独添加这两个类。

this.renderer.addClass(this.el.nativeElement, 'ui');
this.renderer.addClass(this.el.nativeElement, 'leftAlign');

请注意,您应该从内部将 AppComponent 称为 this

export class AppComponent {
public static SemanticUiBaseDir = '../../semantic/dist/';
public getSemanticeUiBaseDir() : string{
return this.SemanticUiBaseDir; // I changed AppComponent to this here
}
public constructor() {}
}

关于angular - 无效字符错误 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45112278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com