TypeScript - Angular: Višelinijski niz
Ja sam početnik u Angular 2 i napisao sam ovaj dio koda u svom 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 "};
public showDetail = false;
onSelect() {
this.showDetail=true;
}
}
Radi, kad odem u preglednik "Max Brown je prikazan".
Sada želim napisati dio predloška u različitim redovima ovako:
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 "};
public showDetail = false;
onSelect() {
this.showDetail=true;
}
}
Ali dobijem ovu pogrešku u Chrome konzoli:
Uncaught TypeError: Cannot read property 'split' of undefined
Odgovor #1
Omotajte tekst u ` (backtickove) umjesto u jednostruke navodnike ', tada može zauzimati više redaka.
var myString = `abc
def
ghi`;
Odgovor #2
Prihvaćeni odgovor radi samo ako želimo da se u našem stringu doda \n, ako samo želimo da sadržaj bude u novom retku bez dodavanja \n u dugom stringu, molimo dodajte\na kraju svake linije ovako
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
Napomena - pobrinite se da ne dodajete tabove i razmake na početku sljedećeg (drugog u primjeru) retka
Odgovor #3
const multiLineString = [
"I wish",
"there were a better way",
"to do this!",
].join(" ");
