Angular 2 template vs. templateUrl?

Angular 2 template vs. templateUrl - What are differences in template and templateUrl?

What are differences of using template and templateUrl in Angular 2 Component?
Angular 2 template vs. templateUrl?
When using template vs. templateUrl?

Inline templates are specified directly in the component using template and it is more complex for bigger templates. As per expert suggestions, use templates and styles into a separate file, when your code more than 5 to 10 lines.

Stayed Informed Bindings in Angular 2 

External templates define the HTML in a separate file and reference this file in templateUrl.

To use a relative path in the templateUrl we must include import component form @angular/core
Some befits for template Urls i.e.
1.               Separations of code
2.               Easy debugging

The upcoming offline template compiler will inline templates linked by templateUrl.

Example for Inline Template -
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule, Http } from '@angular/http';
import { UserService } from '../service/user.service';
import { AppGlobals } from '../../shared/app.globals';

@Component({
    selector: 'users-app',
    template: `<div *ngFor="let user of users; let i = index">
                    <div>{{user.id }}</div>
                    <div>{{user.id | barcodepipe:true}}</div>
                    <div>{{user.login}}</div>
                    <div>{{user.description}}</div>
                    <div><a href="{{user.public_members_url}}" target="_blank">{{user.public_members_url}}</a></div>
              </div>`,
    styleUrls: ['./user.component.css'],
    providers: [UserService, AppGlobals],
})
export class UsersApp {
    //USERS DECLARATIONS.
    users = [];

    //USER COMPONENT CONSTRUCTOR.
    constructor(private _userService: UserService,
        private _global: AppGlobals) { }

    //GET USERS SERVICE ON PAGE LOAD and BIND UI GRID.
    ngOnInit() {
        this._userService.getAPIUsers(this._global.baseAPIUrl + 'users/api/GetUsers').subscribe(data => this.users = data);
    }
}

Example for external templates - Separate file-
import { Component} from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule, Http } from '@angular/http';
import { UserService } from '../service/user.service';
import { AppGlobals } from '../../shared/app.globals';

@Component({
    selector: 'users-app',
    templateUrl: './user.component.html',
    styleUrls: ['./user.component.css'],
    providers: [UserService, AppGlobals],
})
export class UsersApp { 
  //USERS DECLARATIONS.
    users = [];

    //USER COMPONENT CONSTRUCTOR.
    constructor(private _userService: UserService,
        private _global: AppGlobals) { }

    //GET USERS SERVICE ON PAGE LOAD and BIND UI GRID.
    ngOnInit() {
        this._userService.getAPIUsers(this._global.baseAPIUrl + 'users/api/GetUsers').subscribe(data => this.users = data); 
    }
}

I hope you are enjoying with this post! Please share with you friends!! Thank you!!!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^