<html ng-app="presentation"> ... <script src="./lib/jquery.min.js"></script> <script src="./lib/angular.min.js"></script> <script> var app = angular.module('presentation', []); </script>
ng-app
initializes the applicationDemo:
<h1 lang="de" style="display: none;">Einfache Bindings {{demo}}</h1> <h1 lang="en">Simple bindings {{demo}}</h1> <div lang="de" style="display: none;">AngularJS ermöglicht ein Templating direkt im Browser.</div> <div lang="en">AngularJS utilizes templating in the browser.</div> <p>Demo: {{demo}}</p> <input class="form-control" type="text" ng-model="demo">
ng-model
, ng-if
, ng-init
<input type="checkbox" ng-model="state" ng-true-value="'enabled'" ng-false-value="'disabled'" ng-init="state='enabled'"> <p>State: {{state}}</p> <select class="form-control" ng-model="state"> <option value="enabled">enabled</option> <option value="disabled">disabled</option> </select> <h3 ng-if="state == 'enabled'">AngularJS is cool!</h3>
State: enabled
ng-class
<input type="checkbox" ng-model="everyThingOk" ng-init="everyThingOk=true" > <span ng-class="{'alert-success':everyThingOk, 'alert-danger':!everyThingOk }" class="alert">This is the message</span>
ng-style
<button class="btn" ng-click="myStyle={color:'red', 'font-weight':'bold'}">red</button> <button class="btn" ng-click="myStyle={'color':'blue'}">blue</button> <span ng-style="myStyle">Sample text, myStyle={{myStyle}}</span>
$scope
variable.app.controller('BindingsCtrl1', function($scope) { console.log('init controller ..'); $scope.answer = 42; $scope.anyFunction = function () { $scope.question = ' is the answer!'; }});
<div ng-controller="BindingsCtrl1"> <button type="button" class="btn" ng-click="anyFunction()">Click me</button> <h3>{{answer}}{{question}}</h3> </div>
app.controller('FormatterCtrl', function($scope) { $scope.number = 42; $scope.time = new Date(); }); app.filter('custom', function() { return function(value) { return '-' + value +'.00 bli bla'; } })
<div>Number: {{number | number:4}}</div> <div>Currency: {{number | currency}}</div> <div>Custom: {{number | custom}}</div> <div>Time: {{time | date:'medium'}}</div>
ng-repeat
lassen sich einfach Tabellen und Listen aufbauen.ng-repeat
.app.controller('RepeatController', function($scope) { $scope.persons = [ {first: 'Arthur', last: 'Dent'}, {first: 'Zaphod', last: 'Beeblebrox'}, {first: 'Marvin', last: 'robot'}, ]});
<table ng-controller="RepeatController"> <tr ng-repeat="person in persons"> <td>{{person.first}}</td> <td>{{person.last}}</td> </tr> </table>
Arthur | Dent |
Zaphod | Beeblebrox |
Marvin | robot |
<select class="form-control" ng-model="orderField"> <option value="first">first</option> <option value="last">last</option> </select>
<input class="form-control" type="text" ng-model="filterText">
<tr ng-repeat="person in persons | orderBy:orderField | filter: filterText">
Arthur | Dent |
Zaphod | Beeblebrox |
Marvin | robot |
$scope.$watch()
$watch(watchExpression, [listener], [objectEquality]);
$scope.$watchCollection()
$watchCollection(obj, listener);
$scope.$watch('backlogItems', function(newValue, oldValue) { console.log('user has modified "$scope.backlogItems" ..'); .. }, true);
$scope.$apply()
$scope.$apply()
.<svg width="280" height="280" xmlns="http://www.w3.org/2000/svg" version="1.1"> <circle ng-attr-cx="{{circle.x}}" ng-attr-cy="{{circle.y}}" ng-attr-r="{{circle.r}}" style="stroke:#7D7D7D;fill:#EBEBEB"></circle> <line ng-attr-x1="{{circle.x}}" ng-attr-y1="{{circle.y}}" ng-attr-x2="{{sec.x}}" ng-attr-y2="{{sec.y}}" style="stroke:#E16969;stroke-width:1"></line> <line ng-attr-x1="{{circle.x}}" ng-attr-y1="{{circle.y}}" ng-attr-x2="{{min.x}}" ng-attr-y2="{{min.y}}" style="stroke:#E16969;stroke-width:5"></line> <line ng-attr-x1="{{circle.x}}" ng-attr-y1="{{circle.y}}" ng-attr-x2="{{hour.x}}" ng-attr-y2="{{hour.y}}" style="stroke:#E16969;stroke-width:5"></line> </svg>18.06.2025 05:53:33
app.controller('SVGCtrl', function($scope) { var shift = 135; $scope.circle = { r:130, x: shift, y: shift }; function update() { $scope.date = new Date(); var sec = $scope.date.getSeconds() + $scope.date.getMilliseconds()/1000; var min = $scope.date.getMinutes() + $scope.date.getSeconds()/60; var h = $scope.date.getHours()%12 + $scope.date.getMinutes()/60; $scope.sec = { x: shift + 100 * Math.cos(sec/60 * Math.PI * 2 - Math.PI/2), y: shift + 100 * Math.sin(sec/60 * Math.PI * 2 - Math.PI/2)}; $scope.min = { x: shift + 100 * Math.cos(min/60 * Math.PI * 2 - Math.PI/2), y: shift + 100 * Math.sin(min/60 * Math.PI * 2 - Math.PI/2)}; $scope.hour = { x: shift + 40 * Math.cos(h/12 * Math.PI * 2 - Math.PI/2), y: shift + 40* Math.sin(h/12 * Math.PI * 2 - Math.PI/2)}; window.setTimeout(function() {$scope.$apply(update)}, 100); } update(); });
app.provider('helloProvider', function() { this.$get = function() { return { sayHello: function() { return "Hello, Provider!" } } } });
app.factory('helloFactory', function() { return { sayHello: function() { return "Hello, Factory!"; } }});
app.service('helloService', function() { this.sayHello = function() { return "Hello, Service! "; }; });
app.value('helloValue', "Hello, Value! ");
app.controller('DependencyInjection1', function($scope, helloProvider, helloValue, helloService, helloFactory) { console.log(helloValue); console.log(helloProvider.sayHello()); console.log(helloService.sayHello()); console.log(helloFactory.sayHello()); });
app.controller('DependencyInjection2', ['$scope', 'helloService', 'helloFactory', function($scope, helloService, helloFactory) { console.log(helloService.sayHello()) console.log(helloFactory.sayHello()); }]);
app.directive('name', function () { return { restrict: .. template: .. replace: .. templateURL: .. transclude: .. link: .. scope: .. } });
restrict:
restrict:
app.directive('myDirective', function () { return { restrict: 'E', }});
<my-directive ...></my-directive>
<my-directive .../>
does not work properly!restrict: 'A',
<div my-directive=".." ...></div> oder <div my-directive></div>
restrict: 'C',
<div class="my-directive" ...></div>
restrict: 'AE';
restrict: 'AE';
template:
template:
app.directive('myButton', function () { return { restrict: 'E', template: '<button type="button" class="btn">click me</button>', }});
<my-button></my-button>
<my-button><button type="button" class="btn">click me</button></my-button>
replace: true
, the surrounding tag would be replaced.templateURL:
templateURL:
$templateCache
dafür, dies nur einmal zu laden.$templateCache
is responsible for single loading.templateURL: '/myButton.html'
<script/>
-Tag<script/>
tag<script type="text/ng-template" id="/myButton.html"> <button type="button" class="btn">click me</button> </script>
transclude:
transclude:
app.directive('myButton2', function () { return { restrict: 'E', transclude: true, template: '<button type="button" class="btn">\ <span ng-transclude></span></button>', }});
<my-button2>That's the <strong>label!</strong><my-button2/>
link:
link:
link
-Attribut kann beliebiger Code ausführt werden.link
attribute, custom code can be executed.app.directive('myLinkedButton', function () { return { restrict: 'E', template: '<button type="button" class="btn">click me</button>', link: function (scope, elem, attrs) { elem.on('click', function() { alert(attrs.message); }); }}});
<my-linked-button message="42, ist the answer!"></my-linked-button>
scope:
scope:
app.directive('myScopedButton', function () { return { restrict: 'E', scope: { myMessage: '@message', // read only mapping myModel: '=model', // two way binding myAction: '&action', // function reference }, template: '<input class="form-control" type="text" ng-model="myModel">\ <button type="button" class="btn" ng-click="myAction()">{{myMessage}}</button>', }});
<my-scoped-button message="I'm a button!" model="anyVar" action="sayHello()"></my-scoped-button>
$routeProvider
<script type="text/ng-template" id="/defaultTemplate.html"> <div style="background:grey; height: 150px; "><h3>Default Template {{demoData}}</h3></div> </script> <script type="text/ng-template" id="/template1.html"> <div style="background:green; height: 150px;"><h3>Template 1 {{demoData}}</h3></div> </script> <script type="text/ng-template" id="/template2.html"> <div style="background:blue; height: 150px;"><h3>Template 2 {{demoData}}</h3></div> </script>
app.config(function($routeProvider) { $routeProvider .when('/template1', { templateUrl: '/template1.html', }).when('/template2', { templateUrl: '/template2.html', }).otherwise({ templateUrl: '/defaultTemplate.html', }); });
<div ng-view></div>
ui-router
ui-router
app.config(function($stateProvider) { $stateProvider .state('s1', { url: '/s1', views: { 'left' : { templateUrl: '/defaultTemplate.html' }, 'right' : { templateUrl: '/defaultTemplate.html'} } }) .state('s2', { url: '/s2', views: { 'left' : { templateUrl: '/template1.html' }, 'right' : { templateUrl: '/template2.html'} } }); });
<div ui-view="left">x</div> <div ui-view="right">x</div>
<form novalidate="" class="css-form"> <style type="text/css"> #css-form input.ng-invalid.ng-touched { background-color: #FA787E; } #css-form input..ng-touched { background-color: #78FA89; } </style> <input type="text" class="form-control" ng-model="var1" required="" placeholder="this is required"> <input type="text" class="form-control" ng-model="var2" pattern="^\S+$" placeholder="no spaces here"> </form>
$http
app.controller('httpExample', function($scope, $http) { $http.get('/people') .success(function (result) { $scope.httpResult = result; }); });
$resource
item.$save
, item.$delete
<script src="./lib/angular-resource.min.js"/> var app = angular.module('presentation', ['ngResource']);
app.controller('resourceExample', function($scope, $resource) { var Person = $resource('/people/:id', {id:'@id'}); $scope.person = Person.get({id:1}, function() { $scope.person.first = 'new Name'; $scope.person.$save(); }); });
{}
$httpBackend
app.run(function($httpBackend) { var people = [{id: 0, first: 'Arthur', last: 'Dent'}, {id: 1, first: 'Zaphod', last: 'Beeblebrox'}]; $httpBackend.whenGET('/people').respond(people); $httpBackend.whenGET('/people/1').respond(people[1]); $httpBackend.expectPOST('/people/1').respond(function(data){return data}); window.setTimeout(function() { $httpBackend.flush(); }, 1000); });
describe("A suite", function() { beforeEach(function() { ... }); it("contains spec with an expectation", function() { expect(true).toBe(true); }); })