Skip to content

Case study: AngularJS directive to load both JSON data from remote endpoint and template asynchronously to a page.

Notifications You must be signed in to change notification settings

meltinspace/async-template

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Async template directive

AngularJS directive to load both JSON data from remote endpoint and template asynchronously to a page.

See example for some sample usage or try a live demo.

Installation

Simply attach JS file to your page and add 'asyncTemplateModule' module as a dependency ...

var app = angular.module('yourModuleName', ['asyncTemplateModule']);

... and use it in your templates

<async-template endpoint="/api/some_endpoint" template="templates/template.html" />

Usage

Directive parameters

  • endpoint - JSON endpoint (API) URL. REQUIRED.
  • template - template URL (should be local, directive uses HTTP GET only to load template). REQUIRED.
  • error-template - template which is loaded when loading data from endpoint fails.
  • jsonp - boolean switch whether use JSONP method instead of simple HTTP GET.
  • is-array - boolean switch whether data returned by endpoint should be parsed as array.
  • reload-interval - load data periodically in some interval in miliseconds.
  • observe - observe only manually defined list of directive attributes. Directive observes all attributes by default.

Load data using HTTP GET, no endpoint params

Simply loads JSON data from endpoint using HTTP GET method, loads template and compiles it with data

<async-template endpoint="/api/some_endpoint" template="templates/template.html" />

Load data using JSONP, no endpoint params

Simply loads JSON data from endpoint using JSONP method, loads template and compiles it with data

<async-template endpoint="/api/some_endpoint" template="templates/template.html" jsonp="true" error-template="templates/404.html" />

Load data with fixed params

<async-template endpoint="/api/user/:userId/info" template="templates/template.html" userId="1" />

Load data with params binded to application scope

Loads data from endpoint, user ID is taken from app scope - method getUserId(). Directive observes this parameter, so when it's value changes, it reloads appropriate data.

<async-template endpoint="/api/user/:userId/info" template="templates/template.html" user-id="{{getUserId()}}" />

Load data periodically every 1 second

<async-template endpoint="/api/user/:userId/info" template="templates/template.html" user-id="{{getUserId()}}" reload-interval="1000" />

About

Case study: AngularJS directive to load both JSON data from remote endpoint and template asynchronously to a page.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published