简体   繁体   中英

what does the symbol '$' mean in angular?

What does mean by $ in ${some_var}

search(term:string) {
        let promise = new Promise((resolve, reject) => {
        let apiURL = `${this.apiRoot}?term=${term}&media=music&limit=20`;
        this.http.get(apiURL)
            .toPromise()
            .then(
            res => { // Success
                console.log(res.json());
                resolve();
                   }
                 );
        });
        return promise;
    }

Thats template literals to use for string interpolation.

Earlier we use below code for string concatenation

var user ="lokesh"

var testStr = "my name is "+ user 

Now in typescript and in ECMA6 that can be used like this

var user ="lokesh"

var testStr = `my name is ${user}`

In your case old version

 let apiURL = this.apiRoot + '?term='+term+'&media=music&limit=20';

typescript and ECMA6

 let apiURL = `${this.apiRoot}?term=${term}&media=music&limit=20`;

There are other usage of $ sign:

<li *ngFor="let hero of heroes$ | async" >

in this case, The $ is a convention that indicates heroes$ is an Observable, not an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM