Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript - Angular: Multiline string

I'm an Angular 2 beginner and I've written this piece of code in my dev/app.component.ts:

import {Component} from 'angular2/core';  @Component({     selector: 'my-app',     template: '<h3 (click)="onSelect()"> {{contact.firstName}} {{content.lastName}}</h3>' }) export class AppComponent {     public contact = {firstName:"Max", lastName:"Brown", phone:"3456732", email:"[email protected]"};      public showDetail = false;     onSelect() {         this.showDetail=true;     } } 

It works, when I go to the browser "Max Brown is displayed".

Now, I want to write the template part on different lines like this:

import {Component} from 'angular2/core';  @Component({     selector: 'my-app',     template: '<h3 (click)="onSelect()">     {{contact.firstName}} {{contact.lastName}}<h3>' }) export class AppComponent {     public contact = {firstName:"Max", lastName:"Brown", phone:"3456732", email:"[email protected]"};      public showDetail = false;     onSelect() {         this.showDetail=true;     } } 

But I get this error in Chrome console:

Uncaught TypeError: Cannot read property 'split' of undefined 
like image 609
splunk Avatar asked Feb 05 '16 13:02

splunk


People also ask

How do I create a multiline string in TypeScript?

The multiline strings without back-quote are created by inserting a \n newline character (LF). As we said earlier, the /n (LF) character is inserted when you use the template strings.

How do you write a multi line string in JavaScript?

There are three ways to create strings that span multiple lines: By using template literals. By using the + operator – the JavaScript concatenation operator. By using the \ operator – the JavaScript backslash operator and escape character.

How do you write multi line strings in template literals in JavaScript?

There are three ways to create a multiline string in JavaScript. We can use the concatenation operator, a new line character (\n), and template literals. Template literals were introduced in ES6. They also let you add the contents of a variable into a string.

What is the delimiter for multi line strings in JavaScript?

Method 1: Multiline-strings are created by using template literals. The strings are delimited using backticks, unlike normal single/double quotes delimiter.


1 Answers

Wrap the text in ` (backticks) instead of single quotes ', then it can span multiple lines.

var myString = `abc def ghi`; 
like image 107
Günter Zöchbauer Avatar answered Oct 07 '22 02:10

Günter Zöchbauer