Thursday, 5 September 2019

Forms, Input and Services - angular js king

Angular JS Forms, Input, and Services:

Working with ng-model:

In the past part, we saw the ng-tie mandate or its proportionate twofold wavy documentation, which enabled us to take the information from our controllers and show it in the UI. That gives us our single direction information authoritative, which is amazing in its respect. In any case, most applications we grow additionally have client associations and parts where the client needs to encourage in the information. From enlistment structures to profile data, structures are a staple of web applications, and AngularJS gives the ng-model mandate to us to manage sources of info and two-way information authoritative:

<html ng-app="notesApp">
<head><title>Notes App</title></head>
<body ng-controller="MainCtrl as ctrl">
<input type="text" ng-model="ctrl.username"/>
You typed {{ctrl.username}}
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
<script type="text/javascript">
angular.module('notesApp', [])
.controller('MainCtrl', [function() {
this.username = 'nothing';
}]);
</script>
</body>
</html>

In this model, we characterize a controller with a variable uncovered on its case called username. Presently, we get its incentive out into the HTML utilizing the ng-controller and the twofold wavy punctuation for a single direction information official. What we have presented additionally is an information component. It is a plain content box, however, on it, we have connected the ng-model mandate.

Angular JS - Forms
Angular JS - Forms

Leverage Data Binding and Forms:

When designing your forms and deciding which fields to bind the ng-model to, you should always consider what format you need the data in. Let’s take the following example to demonstrate:

<html ng-app="notesApp">
<head><title>Notes App</title></head>
<body ng-controller="MainCtrl as ctrl">
<form ng-submit="ctrl.submit1()">
<input type="text" ng-model="ctrl.username">
<input type="password" ng-model="ctrl.password">
<input type="submit" value="Submit">
</form>
<form ng-submit="ctrl.submit2()">
<input type="text" ng-model="ctrl.user.username">
<input type="password" ng-model="ctrl.user.password">
<input type="submit" value="Submit">
</form>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
<script type="text/javascript">
angular.module('notesApp', [])
.controller('MainCtrl', [function() {
var self = this;
self.submit1 = function() {
// Create user object to send to the server
var user = {username: self.username, password: self.password};
console.log('First form submit with ', user);
};
self.submit2 = function() {
console.log('Second form submit with ', self.user);
};
}]);
</script>
</body>
</html>

There are two forms in this example, both in the same fields. The first form is bound to a username and password directly on the controller, while the second form is bound to a username and password key on a user object in the controller. Both of them trigger an ng-submit function on the submission of a function. Now in the case of the first form, we have to take those fields from the controller and put them into an object, or something similar, before we can send it to the server. In the second case, we can directly take the user object from the controller and pass it around.

The second flow makes more sense because we are directly modeling how we want to represent the form as an object in the controller. This removes any additional work we might have to do when we work with the values of the form.

Form Validation and States:

We have seen how to create forms, and enable (and leverage) data-binding to get our data in and out of the UI. Now let’s proceed to see how else AngularJS can benefit us when working with forms, and especially with validation and various states of the forms and inputs:

<html ng-app="notesApp">
<head><title>Notes App</title></head>
<body ng-controller="MainCtrl as ctrl">
<form ng-submit="ctrl.submit()" name="myForm">
<input type="text"
ng-model="ctrl.user.username"
required
ng-minlength="4">
<input type="password"
ng-model="ctrl.user.password"
required>
<input type="submit"
value="Submit"
ng-disabled="myForm.$invalid">
</form>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
<script type="text/javascript">
angular.module('notesApp', [])
.controller('MainCtrl', [function() {
var self = this;
self.submit = function() {
console.log('User clicked submit with ', self.user);
};
}]);
</script>
</body>
</html>


In this example, we reworked our old example to add some validation. In particular, we want to disable the Submit button if the user has not filled out all the required fields. How do we accomplish this?

  1. We give the form a name, which we can refer to later. In this case, it is myForm.
  2. We leverage HTML5 validation tags and add the required attribute to each input field.
  3. We add a validator, ng-minlength, which enforces that the minimum length of the value in the input field for the username is four characters.
  4. On the Submit button, we add an ng-disabled directive. This disables the element if the condition is true.
  5. For the disable condition, we leverage the form, which exposes a controller with the current state of the form. In this case, we tell the button to disable itself if the form with the name myForm is $invalid.
When you use forms (and give them names), AngularJS creates a FormController that holds the current state of the form as well as some helper methods. You can access the FormController for a form using the form’s name, as we did in the preceding example using myForm. Things that are exposed as the state and kept up to date with data-binding
Now that you have understood basics of Angular JS, Check out the Angular JS internship by Surya Informatics Solutions, a trusted internship company with a network of more than 250,000 satisfied Angular JS Developers spread across the globe.






Sunday, 25 August 2019

Unit Testing In Angular JS

Unit Testing in AngularJS

   Unit testing is taking a solitary utmost or part of our code, and making affirmations and tests to guarantee that it fills in as organized. While phenomenally conventional on the server side, we regularly discover the tendency for making unit tests not right when making clientside applications. Here are five reasons why we should make unit tests when working on a JavaScript-based customer side application: Check of accuracy Without unit tests, there is apparently obstructed dependence on completely manual tests to guarantee that our segment or part of the application genuinely works. Unit tests go about as confirmation that what we have grown genuinely does what it should, that we have managed all the edge cases enough, and that it passes on the benefit understand these cases. Nonappearance of compiler In JavaScript, there isn't the scarcest piece like the Java compiler to reveal to us that something doesn't work. It is dependent upon the program to at long last reveal to us that something is broken, and even by at that point, various ventures produce various outcomes. Unit tests can be run a long time previously our application makes it out to the program to get issues and alarm us about our suppositions.


Angular JS
Angular JS



Catch errors early:

Without unit tests, we would only know about an error in our application after we hit refresh in our browser and saw the live application. Unit tests can help us catch errors much earlier, reducing turnaround time and thus increasing our development speed.


Prevent regressions:

At the end of the day, we are unlikely to be the only person working on our codebase.Other developers will inevitably rely on or actively change parts of the codebase that we developed.You can ensure that they don’t change any fundamental assumptions by providing a set of unit tests that prevent regressions and bugs in the future.

Specification:

Comments have a bad habit of becoming outdated. Unit tests in AngularJS, especially written using Jasmine, look and read like English. And because unit tests break when the underlying code changes, we are forced to keep comments updated. So unit tests can act as a living, breathing specification for our codebase.


Introduction to Karma


  
  Karma is the test runner that makes running tests painless and amazingly fast. It uses NodeJS and SocketIO to facilitate tests in multiple browsers at insanely fast speeds


Explaining the Karma Config:


To use Karma, we need a configuration file that tells Karma how to operate. We will see how easy it is to generate this configuration file in the next section. But first, let’s take a look at the Karma configuration and the options that we will use for our unit tests in this chapter. The default name for this file is karma.conf.js, and unless you tell Karma otherwise, it will automatically look for a file with this name in the directory you run

Karma from:

// File: chapter3/karma.conf.js
// Karma configuration
module.exports = function(config) {
config.set({
// base path that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'angular.min.js',
'angular-mocks.js',
'controller.js',
'simpleSpec.js',
'controllerSpec.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port

Jasmine: Spec Style of Testing


  In the previous section, we saw the test runner that we will use to run the unit tests we write for AngularJS. But the actual testing framework that we will use for the purpose of this book is Jasmine. The Jasmine framework uses what we call a behavior-driven style of writing tests.That is, instead of writing a bunch of functions and asserts, we describe behaviors and set expectations. How does this translate into actual tests? Let’s take a deeper look.


Jasmine Syntax
Jasmine Syntax


Jasmine Syntax


Before we jump down into Jasmine syntax and talk about the various concepts of a Jasmine test, let’s take an example to make things clearer:
// A test suite in Jasmine
describe('My Function', function() {
var t;
// Similar to setup
beforeEach(function() {
t = true;
});
afterEach(function() {
t = null;
});
it('should perform action 1', function() {
expect(t).toBeTruthy();
});
it('should perform action 2', function() {
var expectedValue = true;
expect(t).toEqual(expectedValue);
});
});


Now that you have understood basics of Angular JS, Check out the Angular JS internship by Surya Informatics Solutions , a trusted internship company with a network of more than 250,000 satisfied Angular JS Developers spread across globe.




FOLLOW US ON : YoutubeTwitterFacebookLinkedin





Friday, 9 August 2019

AngularJS Directives and Controllers


Fundamental AngularJS Directives and Controllers


An AngularJS module has two sections to it:

• A module can characterize its own controllers, administrations, manufacturing plants, and mandates. These are capacities and code that can be gotten to all through the module.

• The module can likewise rely upon different modules as conditions, which are characterized at the point when the module is instantiated. This means AngularJS will proceed to discover the module with that specific name, and guarantee that any capacities, controllers, administrations, and so forth characterized in that module are made accessible to all the code characterized in this module.

This is how we define a module named notesApp:
angular.module('notesApp', []);

angular.module('notesApp');

This line of code tells AngularJS to find an existing module named notesApp, and to
make it accessible to utilize, include, or adjust in the present document. same module over various records and add code to it.

Angular JS Development
Angular JS Development


Creating Our First Controller:

 We perceived how to make modules, yet what do we do with them? Up until this point, they have recently been void modules. We should now investigate controllers. Controllers in AngularJS are our workhorse, the

JavaScript capacities that perform or trigger most of our UI-situated work. A few
of the normal obligations of a controller in an AngularJS application include:

• Fetching the correct information from the server for the current UI

• Deciding which parts of that information to show to the client

• Presentation rationale, for example, how to show components, which parts of the UI to appear, step by step instructions to style them, and so forth.

• User associations, for example, what happens when a client clicks something or how a content information ought to be approved

An AngularJS controller is quite often straightforwardly connected to a view or HTML. We will never have a controller that isn't utilized in the UI (that sort of business rationale goes into administrations). It goes about as the passage between our model, which is the information that drives our application, and the view, which is the thing that the client sees and connects with. So how about we investigate how we could approach making a controller for our notesApp

Angular Snippet
Angular Snippet

Module:


<html ng-app="notesApp">
<head><title>Hello AngularJS</title></head>
<body ng-controller="MainCtrl">
Hello {{1 + 1}}nd time AngularJS
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
<script type="text/javascript">
angular.module('notesApp', [])
.controller('MainCtrl', [function() {
// Controller-specific code goes here
console.log('MainCtrl has been created');
}]);
</script>
</body>
</html>

Dependency Injection Syntax and AngularJS


The notation that we have used is one of the two ways in which we
can declare AngularJS controllers (or services, directives, or filters).
The style we have used (and will use for the remainder of the book),
which is also the recommended way, is safe-style of Dependency Injection,
or declaration. We could also use:

angular.module('notesApp', [])
.controller('MainCtrl', function() {
});

and it would work similarly, but it might cause problems when we
have a build step that minifies our code.



Now that you have understood basics of Angular JS, Check out the Angular JS internship by Surya Informatics Solutions , a trusted internship company with a network of more than 250,000 satisfied Angular JS Developers spread across globe.




FOLLOW US ON : YoutubeTwitterFacebookLinkedin

Thursday, 8 August 2019

What is Angular JS?


AngularJS is a super heroic JavaScript MVC structure for the Web. We call it super heroic in light of the fact that AngularJS does as such much for us that we just need to concentrate on our center application and let AngularJS deal with everything else. It enables us to apply a standard,tried-and-tried programming building rehearses customarily utilized on the server-side in customer side programming to quicken front end advancement. It gives a reliable adaptable structure that makes it a breeze to grow huge, complex applications as a feature of a team. And the best part? It's everything done in unadulterated JavaScript and HTML. No compelling reason to become familiar with another new programming or contemplating language. And how can it satisfy all these insane and great, apparently difficult to-fulfill promises? The AngularJS theory is driven by a couple of key precepts that drive everything from how to structure your application, to how your applications ought to be snared together, to how to test your application and coordinate your code with different libraries. Yet, before we get into each of these, how about we investigate why we ought to try and mind in any case.
 
Angular JS
Angular JS

Angular JS Types:

  •     AngularJS mandates are broadened HTML characteristics with the prefix ng-.
  •    The ng-application mandate instates an AngularJS application.
  •      The ng-init mandate instates application information.
  •      The ng-model mandate ties the estimation of HTML controls (input,select, text-area).   

AngularJS Benefits:


We are going to make a few cases in this segment, which we will develop in the following area when we jump into how AngularJS makes this conceivable: 

• AngularJS is a Single Page Application (SPA) meta-system. With customer side templating and substantial utilization of JavaScript, making and keeping up an application can get dull and difficult. AngularJS evacuates the cruft and does the truly difficult work, with the goal that we can concentrate exclusively on the application center. 

• An AngularJS application will require fewer lines of code to finish an errand than an unadulterated JavaScript arrangement utilizing jQuery would. At the point when contrasted with different systems, you will, in any case, wind up composing less standard, and cleaner code, as it moves your rationale into reusable parts and out of your view. 

• Most of the code you write in an AngularJS application will be centered around business rationale or your center application usefulness, and not superfluous everyday practice cruft code. This is an aftereffect of AngularJS dealing with all the standard that you would somehow or another ordinarily compose, just as the MVC engineering design 

. • AngularJS's revelatory nature makes it simpler to compose and get applications. It is straightforward an application's expectation just by taking a gander at the HTML and the controllers. It might be said, AngularJS enables you to make HTMLX (rather than depending on HTML5 or hanging tight for HTML6, and so forth.), which is a subset of HTML that accommodates your necessities and prerequisites. 

• AngularJS applications can be styled utilizing CSS and HTML free of their business rationale and usefulness. That is, it is totally conceivable to change the whole format and plan of an application without contacting a solitary line of JavaScript. 

• AngularJS application layouts are written in unadulterated HTML, so planners will discover it simpler to work with and style them. We are going to make a few cases in this segment, which we will develop in the following area when we jump into how AngularJS makes this conceivable: 

• AngularJS is a Single Page Application (SPA) meta-system. With customer side templating and substantial utilization of JavaScript, making and keeping up an application can get dull and difficult. AngularJS evacuates the cruft and does the truly difficult work, with the goal that we can concentrate exclusively on the application center. 

• An AngularJS application will require fewer lines of code to finish an errand than an unadulterated JavaScript arrangement utilizing jQuery would. At the point when contrasted with different systems, you will, in any case, wind up composing less standard, and cleaner code, as it moves your rationale into reusable parts and out of your view. 

• Most of the code you write in an AngularJS application will be centered around business rationale or your center application usefulness, and not superfluous everyday practice cruft code. This is an aftereffect of AngularJS dealing with all the standard that you would somehow or another ordinarily compose, just as the MVC engineering design.

Angular JS Benefits



.

What Back end Do I Need?


One of the first questions we usually get is regarding the kind of a back end one would need to be able to write an AngularJS application. The very short answer is: there areno such requirements.AngularJS has no set requirements on what kind of a back end it needs to work as a Single-Page Application. You are free to use Java, Python, Ruby, C#, or any other language you feel comfortable with. The only thing you do need is a way of communicating back and forth with your server. Ideally, this would be via X HR (XML HTTP requests) or sockets. If your server has REST or API endpoints that provide JSON values, then it makes your life as a front end developer even easier. But even if your server doesn’t return J SON, that doesn’t mean you should abandon AngularJS. It’s pretty easy to teach AngularJS to talk with an XML server, or any other format for that matter.






Now that you have understood basics of Angular JS, Check out the Angular JS internship by Surya Informatics Solutions , a trusted internship company with a network of more than 250,000 satisfied Angular JS Developers spread across globe.



FOLLOW US ON : Youtube, Twitter, Facebook, Linkedin