类似的问题是这样的: Javascript is not recognize a Flask variable
当我在 vscode 中输入这样的内容时,
<script type="text/javascript">
var x ={{ data }};
</script>
Run Code Online (Sandbox Code Playgroud)
它总是会像这样重新格式化
<script type="text/javascript">
var x = {
{
data
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
不再是 Jinja 变量,并且不能真正在 js 中工作。
那么,vscode中有没有这样的插件可以解决这个问题呢?
我正在使用这个答案将 HTML 字符串转换为 Angular 中的 DOM 元素。
问题是我无法获取 的属性Node。getAttribute()无法使用,因为 typescript 会抱怨No property getAttribute on Node.
代码如下(简化)。
import { AfterViewInit, Renderer2 } from '@angular/core';
export class MockComponent implements AfterViewInit {
constructor(private renderer: Renderer2) {}
private mockString = '<a href="#test1">test 1</a>'
ngAfterViewInit(): void {
const template = this.renderer.createElement('template');
template.innerHTML = this.mockString.trim();
const anchorNodes: NodeList = template.content.querySelectorAll('a');
const anchors: Node[] = Array.from(anchorNodes);
for (const anchor of anchors) {
// how to get attributes of anchor …Run Code Online (Sandbox Code Playgroud) 当我的后端向我发送404错误(URL 有效,只是因为找不到资源,例如http://localhost:4200/post/title-not-exist)时,我希望 Angular 重定向到我的NotFoundComponent,而不更改浏览器中的 URL。
代码如下(简化):
\n\nconstructor(private router: Router, private location: Location) {}\n\nhandleError(err: HttpErrorResponse) {\n switch (err.status) {\n case 404:\n url = this.router.routerState.snapshot.url;\n // \'**\' is set in the app-routing.module.ts\n // { path: \'**\', component: NotFoundComponent }\n this.router.navigate([\'**\'], { replaceUrl: false });\n // weird here too\n // without the setTimeout(), replaceState will not work\n setTimeout(() => {\n this.location.replaceState(url);\n });\n break;\n\n default:\n break;\n }\n return throwError(err);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n现在,我可以重定向到NotFoundComponent并且 URL 没有更改,问题是:
<body>并且<head>可以通过注入在 Angular 组件中获取标签DOCUMENT,如下所示:
import { DOCUMENT } from '@angular/common';
import { Inject } from '@angular/core';
export class TestComponent {
constructor(
@Inject(DOCUMENT) private document: Document
)
// get <head>
// this.document.head
// get <body>
// this.document.body
}
Run Code Online (Sandbox Code Playgroud)
但是可以<html>在 Angular 组件中获取标签吗?