Chao Yang

Nothing seek, nothing find


  • Home

  • Minibooks

  • Projects

  • Résumé

  • Archive

  • About

  • Search
close

Get started with AngularJS

Published at: 2015-05-18   |   Categories: Javascript     |   Reading: 426 words ~2min

AngularJS concepts

angularjs-conception

Core concepts:

  • 2-way data binding
  • directive
  • expression & filter
  • view / template
  • controller

2-way data binding

jogjajs-single-page-application-nganggo-angularjs-17-638

directives

  • manipulate DOM: DOM manipulation should only happen in directive implementations
  • manipulate data Elements: <ng-xx> </ng-xx>

Attributes: <span ng-xx=“exp”></span>

Comments: <!– directive: ng-xx exp –>

Classes: <span class=”ng-xx: exp“></span>

Scope

Scope is a glue between controller, template and model. It keeps models and views separate, but in sync.

Controller

Controller is for UI logic

Injector

The injector uses recipes to create two types of objects, services and specialized objects.

  • Services are objects whose API is defined by the developer writing the service.
  • Specialized objects conform to a specific Angular framework API. These objects extend the framework as plugins and therefore must implement interfaces specified by Angular. These interfaces are Controller, Directive, Filter and Animation.

Recipes

There are five recipe types that define how to create objects: Value, Constant, Factory, Service and Provider.

Value, Constant, Factory and Service are the synaptic sugar of Provider.

  • Value recipe
myApp.value('clientId', 'a12345654321x');

 

  • Constant recipe
myApp.constant('planetName', 'Greasy Giant');
  • Factory recipe
myApp.factory('clientId', function clientIdFactory() {
  return 'a12345654321x';
});
  • Service recipe
function UnicornLauncher(apiToken) {

  this.launchedCount = 0;
  this.launch = function() {
    // Make a request to the remote API and include the apiToken
    ...
    this.launchedCount++;
  }
}

//myApp.factory('unicornLauncher', ["apiToken", function(apiToken) {
//  return new UnicornLauncher(apiToken);
//}]);

myApp.service('unicornLauncher', ["apiToken", UnicornLauncher]);
  • provider recipe
myApp.provider('unicornLauncher', function UnicornLauncherProvider() {
  var useTinfoilShielding = false;

  this.useTinfoilShielding = function(value) {
    useTinfoilShielding = !!value;
  };

  this.$get = ["apiToken", function unicornLauncherFactory(apiToken) {

    // let's assume that the UnicornLauncher constructor was also changed to
    // accept and use the useTinfoilShielding argument
    return new UnicornLauncher(apiToken, useTinfoilShielding);
  }];
});

myApp.config(["unicornLauncherProvider", function(unicornLauncherProvider) {
  unicornLauncherProvider.useTinfoilShielding(true);
}]);
Features / Recipe type Factory Service Value Constant Provider
can have dependencies yes yes no no yes
uses type friendly injection no yes yes* yes* no
object available in config phase no no no yes yes**
can create functions yes yes yes yes yes
can create primitives yes no yes yes yes

Specialized object

The instructions for the injector to create these special objects (with the exception of the Controller objects) use the Factory recipe behind the scenes.

Since the directives are registered via the Factory recipe, we can use the same syntax as with factories.

myApp.directive('myPlanet', ['planetName', function myPlanetDirectiveFactory(planetName) {
  // directive definition object
  return {
    restrict: 'E',
    scope: {},
    link: function($scope, $element) { $element.text('Planet: ' + planetName); }
  }
}]);

Using Factory recipes, you can also define Angular’s filters and animations, but the controllers are a bit special.

myApp.controller('DemoController', ['clientId', function DemoController(clientId) {
  this.clientId = clientId;
}]);

Unlike services, controllers are not singletons.

 

JavaScript scopes and context
SCEA architect
微信扫一扫交流

标题:Get started with AngularJS
作者:Chao
关注:richdyang(CHAO)
声明:自由转载-非商用-非衍生-保持署名(创作共享3.0许可证)

  • Table of Content
  • Site Information
Chao

Chao

Programmer & Life explorer

138 Blogs
49 Categories
20 Tags
GitHub Linkedin
      • AngularJS concepts
        • 2-way data binding
        • directives
        • Scope
        • Controller
      • Injector
        • Recipes
        • Specialized object
© 2009 - 2018 Chao Yang
Powered by - Hugo v0.30.2
Theme by - NexT