Was ist angular?

  • open-source Javascript MVC framework
  • Maintained by Google
  • 100% Clientseitig
  • Nicht gebunden an 3rdparty framework wie jQuery oder Underscore. (Keine Abhängigkeiten)
  • Entstanden 2009

 

Thinking in AngularJS?

  • Keine DOM Manipulationen
  • Focus liegt auf dem View (declarative programming)
  • Views bestehen aus directives (ng-app, ng-init...)
  • Directives verarbeiten die ganzen Events im Hintergrund (i.G.z. jQuery wo DOM-Elemente über Selektoren an Events gebunden werden)
    - dynamische Data-Bindings
  • $scope ist die Schnittstelle zwischen Controller und View
  • jQuery sollte nicht zusammen mit AngularJS verwendet werden
  • Da Logik von View getrennt ist, sind AngularJS Applikation einfach wartbar und testbar (z.B. Testing mit Jasmine Framework)

Directives

<!DOCTYPE html>
<html>
<body ng-controller="sayHello">
. . .
</body>
</html>

Eine Markierung auf einem HTML-TAG, die Angular auffordert Javascript zu referenzieren oder auszufüren.

 

function sayHello(){
    alert("Hello Simon");
}

Controllers

In einem Controller wird mittels Variablen und Funktionen das Verhalten der Applikation gesteuert.

var myApp = angular.module('myApp',[]);

myApp.controller('DoubleController', ['$scope', function($scope) {
  $scope.double = function(value) { return value * 2; };
}]);
<div ng-controller="DoubleController">
  Two times <input ng-model="num"> equals {{ double(num) }}
</div>

Services

Angular services are substitutable objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app.

// Simple GET request example :
$http.get('/someUrl').
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

Buil-in Services (beginnen mit $).

Z.B. $http

jQuery.get() = $http.get()

jQuery.post() = $http.post()

Scopes

Der Scope ist das Verbindungs- stück zwischen View und Controller.

Module

Ein Module ist ein Container der die verschiedenen Teile (controllers, services, filters, directives, etc.) einer Applikation enthält.

Page-Refresh

SEO

Seit 2014

 

Demos

.directive('myIMG', function() {
    return {
      restrict: 'E',
      templateUrl: 'img.html',
      template: "<img src='scope.path'>",
      transclude: true,
      scope: {
          path: '='
      },
      link: function(scope, element, attr) {

      }
    };
  });
<myIMG path=''></myIMG>

HTML

Directives

AngularJS

By Simon Schärer

AngularJS

  • 1,875