Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

Angular - Css and notation related to Array with [] brackets

I have a few lines of code taken from an Italian Angular book entitled "Angular 100% operational" that I don't understand. The text reports to paragraph 5.4: "It often happens that the values to be dynamically added are retrieved from an external source that packs them into an array. In Angular it is also possible to use the following notation:"

@ Component({
    selector: 'ca-news',
    template: `<p [ngClass]="mieclassi" class="big-text"></p>`,
    styleUrls: ['./miostile.css']
}
) export class NewsComponent {
    mieclassi: Array < string > ;
    constructor() { // applico due attributi di classe 
        this.mieclassi = ['text-center', 'light-blue'];
    } 
}

But is it correct in your opinion to insert square brackets here?

this.mieclassi = ['text-center', 'light-blue'];

Because in the previous example of the book, the author didn't insert them, the book indicates:

@Component({
    selector: 'ca-news',
    template: `<p [ngClass]="mieclassi" class="big-text">News con testo molto piccolo</p>`,
    styleUrls: ['./miostile.css']
}) export class NewsComponent {
    mieclassi: string;
    constructor() {(
             this.mieclassi = 'text-center light-blue';
        }
    }
}

and it works! So what's the meaning of [] in the first example?

this.mieclassi = ['text-center', 'light-blue'];

Then the author writes about the use of Css in an Array and introduces this notation with square brackets which, however, does not work for me, this is the compiler's answer:

Failed to compile. src/app/news-component-css/news-component-css.component.ts:17:5 - error TS2322: Type 'string[]' is not assignable to type 'string'.

this.mieclassi = 'text-center light-blue';

Thanks for any suggestions, greetings from Italy

question from:https://stackoverflow.com/questions/66050410/angular-css-and-notation-related-to-array-with-brackets

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...