Angular JS Magazine

duminică, 24 august 2014

Working with Partial Views

The ng-include directive is very useful when you need to take a fragment of HTML content from the server, compile it to process all directives, and add it to the DOM:

The piece of HTML that will be included:

<table class="table">
    <tr ng-repeat="item in atp">

The page where the above HTML will be included using ng-include directive:

<!DOCTYPE html>
<head lang="en">
    <meta charset="UTF-8">
    <script src="../angular/angular.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet" />
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet" />
<div ng-app="" ng-init="atp=[
     { name: 'Nadal, Rafael (ESP)', rank: 1 },
     { name: 'Djokovic, Novak (SRB)', rank: 2 },
     { name: 'Federer, Roger (SUI)', rank: 3 },
     { name: 'Wawrinka, Stan (SUI)', rank: 4 },
     { name: 'Ferrer, David (ESP)', rank: 5 }]">

    <div id="atpPanel" class="panel">
        <h3 class="panel-header">ATP SINGLES RANKINGS</h3>
        <ng-include src="'table.html'"></ng-include>

The attributes of ng-include are:

src -  specifies the URL of the content to load
onload - specifies an expression to be evaluated when the content is loaded
autoscroll - specifies whether AngularJS should scroll the viewport when the content is loaded