168

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
1

3 Answers 3

361

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

var myString = `abc
def
ghi`;
Sign up to request clarification or add additional context in comments.

10 Comments

Seems you can use normal quotes stackoverflow.com/questions/805107/…. Backticks also allow interpolation developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
how can I ignore spaces and new lines?
I'm guessing @SunilGarg wants to add line breaks to the string so it looks good in code, but doesn't want the extra whitespace in the string itself.
That would be the opposite of what my answer is about. I think that would require 'a b c' + 'd e f'
This has the problem that from the second line there must not be any indentation which may look awkward in deeply nested code.
|
29

The accepted answer works only if we want \n added in our compiled string. If we just want the code to be written on a newline, without adding \n in the compiled long string, please add \ at the end of every line like this:

string firstName = `this is line 1 \
and this is line 2 => yet "new line" are not \
included because they were escaped with a "\"`;

console.assert(firstName == 'this is line 1 and this is line 2 => yet "new line" are not included because they were escaped with a "\"'); // true

Note - Make sure you are not adding any tabs or spaces at the beginning of the next (second in the example) line, because they will be added into the compiled string.

Another way is to use the method "string1" + "string2", where it disregards any spaces or tabs at the beginning of the next line:

string longLine = "This is line 1 "
     + " and this is also one line 1";
// The compiled string = "This is line 1 and this is also line 1"

Comments

13
const multiLineString = [
  "I wish",
  "there were a better way",
  "to do this!",
].join(" ");

2 Comments

This is the cleanest looking solution for building xpath selectors in my code. Note: the join method defaults to separating strings with a comma ','. You probably want to use .join("") .
Or, for strings that naturally split on spaces, .join(" ")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.