2

I have this code which works as expected;

import {Component} from 'angular2/core';

@Component({
    selector: 'media-tracker-app',
    templateUrl: 'app/app.component.html',
    styles: ['      h1 {            color: #ffffff;     }']
})

export class AppComponent {}

but if I put some new lines in to make the CSS more readable like this:

import {Component} from 'angular2/core';

@Component({
    selector: 'media-tracker-app',
    templateUrl: 'app/app.component.html',
    styles: ['      h1 {            
        color: #ffffff;     
    }']
})

export class AppComponent {}

... then it breaks my code.

In the console I get: SyntaxError: unterminated string literal

I am just starting to learn Angular 2... any ideas?

2 Answers 2

6

That's totally unrelated to either TypeScript or AngularJS: JavaScript strings using ' and " are not multilines.

You have two options:

  • Use a backslash to escape end of line

var a = 'hey\
you';
  • Use ES6 template strings (using backticks):

var a = `hey
you`;
Sign up to request clarification or add additional context in comments.

4 Comments

thanks guys, yes I was using the wrong syntax. I did not realise that it was a backtick and not a quote coming from PHP background.. Thanks
not always easy to remember the tiny differences between each languages. If that answer helped you, feel free to mark as resolved.
yeah thanks picking up all the little differences all the time! I tried to mark your answer as complete, but it would not count the point because my profile is not good enough yet! Thanks again
rather than the up arrow, I think you should see a green mark/check under it to accept an answer
0

Can you try putting text in ` (backticks) instead of single quotes '?

You can check this link:

template: `
  <h1>{{title}}</h1>
  <h2>My favorite hero is: {{myHero}}</h2>

The template is a multi-line string within ECMAScript 2015 backticks (). The backtick () — which is not the same character as a single quote (') — has many nice features. The feature we're exploiting here is the ability to compose the string over several lines, which makes for much more readable HTML.

Comments

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.