Commenting (out) code in Angular2 TypeScript

前端 未结 3 1958
南笙
南笙 2021-01-05 03:37

I have the following Angular2 TypeScript code with a section commented out as per Javascript convention:

@Component({
    selector: \'my-app\',
    template:         


        
相关标签:
3条回答
  • 2021-01-05 04:24

    If you are in the template, use the HTML comment <!-- ... -->:

    @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>
      `
    })
    
    0 讨论(0)
  • 2021-01-05 04:26

    /* */ is typescript comment delimiter

    They don't work inside a string literal.

    You can use HTML comment syntax instead <!-- -->.

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

    0 讨论(0)
  • 2021-01-05 04:41

    Does not seem to work, though, because it only hides the HTML, while still trying to execute the typescript code inside the commented section.

    0 讨论(0)
提交回复
热议问题