Wednesday, June 15, 2016

Using AngularJS

Using AngularJS



So we've got a lot of the theory behind Angular down; it's time to actually put it in
place. Once we've got our application working, we'll take a look at how we can make
it shine with Bootstrap.

Let's open that index.html file again, but this time also open it up in your browser so we can see what we're working with. This is what we've got so far:
<html lang="en" ng-app>
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/bootstrap.min.css">
  <title></title>
  <script type="text/javascript" src="js/angular.min.js"></script>
</head>
<body>
</body>
</html>
So, we've got Bootstrap and Angular there and we've initialized our app with the  ng-app attribute in the opening <html> tag; let's get cracking.We're going to have a Hello, World app with a bit of a difference. Instead of saying hello to the world, we're going to have an input field that will bind the data and echo it out in our view automatically, and we're going to do all of this without writing a
line of JavaScript.
Let's start out by getting an <h1> tag in our <body> tag:
<h1>Hello, World</h1>
If you view this in your browser, you should notice that Bootstrap has tidied up the default. We no longer have Times New Roman but instead Helvetica and those excess margins around the edge have been removed:




Installing AngularJS

Installing AngularJS


Okay, now that we've got Bootstrap included in our web app, we need to install Angular. Visit https://angularjs.org/ and click on the Download button. You'll be presented with a few options; we want the minified stable version.Copy the downloaded file over to your project's js directory and open up  your index.html file. Angular can be included in your app just like any other
JavaScript file.It's recommended that Angular is included in the <head> tag of your page or  certain functions we'll be taking advantage of throughout the course of the book won't work. While it's not necessary, there will be extra steps you'll need to take if you choose to load Angular further down your HTML file. Pop this <script> tag within the <head> of your page.
<script src="js/angular.min.js"></script>
Ready to go? Well, almost. We need to tell Angular that we want to utilize it in our app. Angular calls this bootstrapping and the framework makes this extremely simple for us. All we need to do is include an additional attribute in our opening
<html> tag:
<html lang="en" ng-app>
That's it! Angular now knows we want to take advantage of it.