Aysegul Yonet / @AysegulYonet
forked by: Gordon Bockus / @gordonk66
Slides: http://bit.ly/1EMISPs
Or if you prefer use this plunker / Exercise Plunker
AngularJS lets you extend HTML vocabulary for your application.
ExampleMVVC
You can bootstrap your angular app by adding ng-app to any html tag.
Only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. To run multiple applications in an HTML document you must manually bootstrap them using angular.bootstrap instead. AngularJS applications cannot be nested within each other.You can think of a module as a container for the different parts of your app – controllers, services, filters, directives, ...
var app = angular.module('myApp', []);Modules makes your code, more readable, maintainable and testable.
var app = angular.module('myApp', []); app.controller("FirstController", ['$scope', function($scope) { $scope.message = 'Hello World!'; //Cool functions goes here... });It is important that the name starts with a capital letter, camel case and Ends with Controller. Controllers are view agnostic. Both controllers and directives have reference to the scope, but not to each other.
Scope is the glue between application controller and the view. Everything you need to be available inyour html, you have to include it in your $scope object.
Scopes are arranged in hierarchical structure which mimic the DOM structure of the application. Scopes provide APIs ($watch) to observe model mutations. ($apply) to propagate any model changes through the system into the view from outside of the "Angular realm" (controllers, services, Angular event handlers).Filters are used for formatting data displayed to the user.
You can create your own to do anything.
angular.module('app') .filter('numberFormat', [function () { return function (number) { if (number !== undefined) { var abs = Math.abs(number); if (abs >= Math.pow(10,12)) {//trillion number = (number / Math.pow(10, 12)).toFixed(1)+ 't'; } else if (abs < Math.pow(10,12) && abs >= Math.pow(10, 9)) {//billion number = (number / Math.pow(10, 9)).toFixed(1)+ 'b'; } else if (abs < Math.pow(10, 9) && abs >= Math.pow(10, 6)) {//million number = (number / Math.pow(10, 6)).toFixed(1)+ 'm'; } else if (abs < Math.pow(10, 6) && abs >= Math.pow(10, 3)) {//thousand number = (number / Math.pow(10,3)).toFixed(1)+ 'k'; } } return number; }; }]);
Directives let you invent new HTML syntax, specific to your application.
Angular comes with great directives you can use.
If jQuery is available, angular.element is an alias for the jQuery function. If jQuery is not available, angular.element delegates to Angular's built-in subset of jQuery, called "jQuery lite" or "jqLite."
What I am trying to say is you use angular.element no matter what.A linking function is enough to create a directive.
var app = angular.module('app', []) app.directive('myDirective', [function(){ return { link: function() { //Do something here. } } }])
Linking function has access to:
var app = angular.module('app', []) app.directive('myDirective', [function(){ return { restrict: "E", //Defaults to "A" for attribute. link: function(scope, element, attrs) { //Do something here... } } }])Example
app.directive('superwoman', [function(){ return { restrict: "E", link: function(scope, element, attrs) { element.bind('mouseenter', function(){ alert("I'm busy saving the world. Come back later.") }) } } }])Example
app.directive('classy', [function(){ var link = function(scope, element, attrs) { element.bind('mouseenter', function(){ element.addClass(attrs.classy); }) } return { restrict:"A", link: link } }])Example
app.controller('SuperController', ['$scope', function($scope){ $scope.totalSuperwomen = 20; $scope.addOne = function() { $scope.totalSuperwomen ++; } }]) app.directive('superwoman', [function(){ return { restrict: "E", link: function(scope, element, attrs) { element.bind('mouseenter', function(){ scope.addOne(); scope.$apply(); }) } } }])Example A Better Way
angular.module('directives.searchbutton', []) /** * Controller used to drive the searchbutton directive. */ .controller('SearchButtonCtrl', function($scope) { // Clear the current search property. $scope.clearSearch = function() { $scope.query = ''; }; }) .directive('searchbutton', function() { return { template: /* should be a string */ <form ng-submit="search()"> <div class="input-group stab-search-button"> <input type="search" class="form-control stab-search-text" placeholder="{{ 'SEARCH_EMAIL' | i18n }}" ng-model="query" ng-focus="focus=true" ng-blur="focus=false"> <span class="stab-search-close icon-circle-close" ng-show="query != ''" ng-click="cancel()"></span> <span class="input-group-btn"> <button class="btn btn-default icon-search-white stab-btn-icon hidden-xs" type="button" ng-disabled="query == '' || query.length <= 2" ng-click="search()"> </button> </span> </div> </form> /* end of should be a string */, scope: { query: '=query', focus: '=focus', blur: '=blur', search: '&onSearch', cancel: '&cancelSearch' }, controller: 'SearchButtonCtrl' }; }); /* example usage */ <div searchbutton="" data-query="data.searchTerm" data-on-search="doSearch()" data-cancel-search="exitSearch()" data-focus="data.searchFocus"> </div>
The service factory function generates the single object or function that represents the service to the rest of the application.
You can use services to organize and share code across your app.The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise with two $http specific methods: success and error.
// 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. });
// Simple POST request example (passing data) : $http.post('/someUrl', {msg:'hello word!'}). 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. });
The service factory function generates the single object or function that represents the service to the rest of the application.
Example Services are often used for getter and setter functions.Dependency Injection is a software design pattern that deals with how components get hold of their dependencies.
var app = angular.module('myApp', [ 'ui.router', 'myControllers' ]); app.controller('MainController', ['$scope', '$http', function($scope, $http){ // }])VERY IMPORTANT, order of dependencies matter. $scope is always first.
The companion suite(s) to the AngularJS framework.
var app = angular.module('myApp', [ 'ngRoute', 'myControllers', 'ui.map' ]);
Chrome developer tools for AngularJS
On your console:
//Gives you the last selected element on your html var lastElement = angular.element($0); //Gives you what is in your scope(functions, variables...) lastElement.scope();
Get a handle on the bootstrapped element
angular.element(document.body).injector().get('$http')({method: 'POST', url: '/post/ajax/stuff'}).then(function(result) {console.log(result);});
AngularJS 1.3 provides animation hooks for common directives such as ngRepeat, ngSwitch, and ngView, as well as custom directives via the $animate service.