610 likes | 710 Vues
Join Trifon Statkov, a seasoned software developer and co-owner, for a one-hour crash course on Angular JS. This presentation covers the basics, building blocks, and practical coding tips to boost your confidence with Angular JS in no time. From the introduction to advanced concepts like MVC architecture and two-way data binding, learn how to streamline your development process and enhance your web applications. Let Trifon guide you through the essentials of Angular JS and level up your skills today.
E N D
From zero to HERO with TrifonStatkov
About me • TrifonStatkov • Software developer at • Co-owner at • @Tricho340
The “Plan” • I will make you feel confident using Angular JS in less than 60 minutes • Presentation comprises of 3 parts: • Part I. Absolute introduction to Angular JS • Part II. Angular JS building blocks • Part III. Some coding
Angular JS facts (1) • Open-source JavaScript framework • Developed in 2009 by MiškoHevery and Adam Abrons • Maintained by Google • Actively developed and supported
Why use it? • The problem – HTML is great for static pages, but has no tools for web applications • The solution – extend and adapt HTML vocabulary with some additional declarations that are useful for web applications
More benefits • Less boilerplate code • Less effort in mundane tasks allowing for better focus on what is ACTUALLY VALUABLE – THE LOGIC • More efficiency in development • Separation of concerns
Some more details • Uses JQLite (a subset of JQuery) for DOM manipulations • If you include jQuery before Angular, it will be used instead of JQLite
Angular JS is MVC • MVC = Model-View-Controller • Less dependencies • Improves maintainability • It is easier to read and understand code
M for Model • Holds the data • Notifies the View and the Controller for changes in the data • Does not know about the View and the Controller
V for View • Displays stuff (buttons, labels, …) • This is what your users will see • Knows about the Model • Usually displays something related to the current state of the Model
C for Controller • Controls everything • Knows about both the Model and the View • The “logic” resides in it • What to happen, when and how
Two-way data binding • View is updated automatically when the Model is changed • Model is updated automatically when a value in the View has changed • No DOM manipulation boilerplate needed!
Dependency Injection • No more “global” objects • Classes will get the objects they need (their dependencies) in order to work
Anatomy of an Angular application (1) • Templates • Routes • Animations • Controllers • Models • Services • Directives • Modules • Filters • Factories • Scopes
Controllers in Angular JS • Define the application’s behaviour
What to put in the Controller? • Focus on the application logic • Don’t worry about the Model – Angular JS will take care of it
The Scope object ($scope) (1) • One scope for each controller • The GLUE between the View and the Controller • A hash of key/values • Holds the data to display in the View • $rootScope object – visible in all controllers
Dispatching event from parent scope to child scope $scope.$broadcast(‘event_name’, (optional) array of arguments topass toeventlisteners); // where $scope is the parent scope
Detecting events dispatched from parent scope in the child scope $scope.$on(‘event_name’, function(args) { // code to invoke if the event occurs }); // where $scope is the child scope
Dispatching events from child scope to parent scope $scope.$emit(‘event_name’); // where $scope is the child scope
Detecting events dispatched from child scope in the parent scope $scope.$on(‘event_name’); // where $scope is the parent scope
What is Angular module? (1) • Container for • Controllers • Services • Directives • Factories • Filters • Configuration information
What is Angular module? (2) • Each Angular JS app contains at least one module • Discrete logical part of the application
What is Angular directive? (1) • Use them when you have to make DOM manipulations • Ever wanted to create your own tag or attribute – this is how you do it! • Takes you one step closer to creating domain-specific markup
What is Angular directive? (2) • Directives should not change a model or controller’s logic • All they handle is creating the page’s content and structure of elements
What is Angular filter? (1) • Reusable operation using which you can modify the content that is shown on the page • Examples: uppercase a value, filter search results, etc.
Defining a new filter module.filter(“filter_name”, function(data) { // data is filtered or modified // in a specific manner and returned return data; }
Using the filter we just defined <div data-ng-repeat= “record in records | filter: filter_name” > </div>
Providers (1) • 4 types • Factory • Service • Value • Provider
Providers - Factory • We define a function that returns an object to which we have attached methods and properties that will be accessible by factory users later • This object is available everywhere in the module in which the factory was defined via Dependency Injection
Factory example (1) – defining the factory module.factory(‘factory_id’, function() { return { functionname: function() { return “this is a function”; }, anotherfunction: function() { // make a request and get data from backend return data; } } });
Factory example (2) – using the factory Module.controller(‘ControllerName’, function ControllerName($scope, factory_id) { $scope.methodname = function() { factory_id.functionname(); } });
Providers - Service • You define a function in which additional functions and properties are defined via the this keyword
Service example (1) - defining a service module.service(‘service_name’, function() { this.function_name = function() { return “this is a function’s result”; }; this.anotherfunction = function() { // make a request to backend // and fetch data return data; });
Service example (2) - using a service in a controller Module.controller(‘ControllerName’, function ControllerName($scope, service_name) { $scope.methodname = function() { service_name.function_name(); } });
Providers - value • Similar to constants • Could be used to store configuration properties
Value example (1) - defining a value module.value(‘value_name’, ‘value’);
Value example (2) - using a value in a controller Module.controller(‘ControllerName’, function ControllerName($scope, value_name) { $scope.methodname = function() { if (value_name == ‘1’) { // do something based on // specific value of the constant } } });
Providers - provider • Define $get method in a function that returns the object to be injected • The object can have various properties and methods similar to the object returned by factory
Provider example (1) - defining a provider module.provider(‘provider_name’, function() { this.$get = function() { return { function_name: function() { }, another_function: function() { } } });
Provider example (2) - Using a provider Module.controller(‘ControllerName’, function ControllerName($scope, provider_name) { $scope.methodname = function() { provider_name.function_name(); } });
Angular JS best practice #1 • Create a separate for module for • Services • Controllers • Filters • Parts are more isolated and decoupled • Easier to test a part of the application
Angular JS best practice #2 • Write some tests using Jasmine
What is Angular module? • Container for • Services • Directives • Factories • Filters • Configuration information