Javascript 在 Angular2 TypeScript 中注释(出)代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36396765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Commenting (out) code in Angular2 TypeScript
提问by What's in a Google Search
I have the following Angular2 TypeScript code with a section commented out as per Javascript convention:
我有以下 Angular2 TypeScript 代码,其中有一部分按照 Javascript 约定注释掉了:
@Component({
selector: 'my-app',
template:
`<h1>{{title}}</h1>
<h2>{{lene.name}}</h2>
<div><label>id: </label>{{lene.id}}</div>
/*<div>
<label>name: </label>
<input [(ngModel)]="lene.name" placeholder="name">
</div>*/`
<div><label>description: </label>{{lene.description}}</div>
})
However, once the TypeScript compiles to Javascript I get the following output to my web browser:
但是,一旦 TypeScript 编译为 Javascript,我的 Web 浏览器就会得到以下输出:
I've searched the API docs and can't find an entry specifying the syntax for this quite basic feature. Anyone know how you do multi-line comments in TypeScript?
我搜索了 API 文档,但找不到指定此非常基本功能的语法的条目。有人知道在 TypeScript 中如何进行多行注释吗?
回答by Günter Z?chbauer
/* */
is typescript comment delimiter
/* */
是打字稿注释分隔符
They don't work inside a string literal.
它们在字符串文字中不起作用。
You can use HTML comment syntax instead <!-- -->
.
您可以改用 HTML 注释语法<!-- -->
。
@Component({
selector: 'my-app',
template:
`<h1>{{title}}</h1>
<h2>{{lene.name}}</h2>
<div><label>id: </label>{{lene.id}}</div>
<!-- <div>
<label>name: </label>
<input [(ngModel)]="lene.name" placeholder="name">
</div> -->'
<div><label>description: </label>{{lene.description}}</div>
})
The HTML commented out this way still is added to the DOM but only as comment.
以这种方式注释掉的 HTML 仍然会添加到 DOM 中,但只是作为注释。
回答by Thierry Templier
If you are in the template, use the HTML comment <!-- ... -->
:
如果您在模板中,请使用 HTML 注释<!-- ... -->
:
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<h2>{{lene.name}}</h2>
<div><label>id: </label>{{lene.id}}</div>
<!-- div>
<label>name: </label>
<input [(ngModel)]="lene.name" placeholder="name">
</div-->
<div><label>description: </label>{{lene.description}}</div>
`
})
回答by hannodb
Does not seem to work, though, because it only hides the HTML, while still trying to execute the typescript code inside the commented section.
但是,似乎不起作用,因为它只隐藏了 HTML,同时仍在尝试执行注释部分中的打字稿代码。