Home > Web Front-end > JS Tutorial > body text

How to develop web applications with angularjs? Angularjs development web application examples

寻∝梦
Release: 2018-09-06 15:06:09
Original
1151 people have browsed it

This article mainly introduces how angularjs develops a simple web application. If you want to know, hurry up and take a look. It introduces in detail how to develop web applications with angularjs. Next, let’s take a look at the article

Let’s first get familiar with the strength of angularjs:

At present, different types of web developers are widely using AngularJS. This set of excellent The framework has also fully demonstrated its ability to meet various needs. As a web developer, whether you are just getting started or have rich practical experience, choosing an excellent framework is a necessary prerequisite for work, and AngularJS is such an ideal solution. In the process of using AnguarJS, you can learn more about application development and how to build better and more attractive application results. If you want to adopt best practices in creating applications, AngularJS can also be of great help. All in all, the powerful functions and features of this framework will never disappoint friends with application development needs.

AngularJS has many outstanding features. Today we will take a simple application as an example to help you understand how to use it. With Firebase, our simple but practical application can be easily built. As a finished product, the developed app allows everyone to log in at any time or log in and publish articles on it.

This is an introduction to AngularJS and Firebase:

AngularJS is currently the most popular JavaScript MVC framework among web developers. If you want to create a unique application, then it is definitely the best choice for you - thanks to its powerful HTML function extension features. With the help of AngularJS, we no longer need to use a lot of code to build applications. Its amazing correlation injection and binding mechanism will make application development extremely convenient.

On the other hand, Firebase provides excellent support for AngularJS, which saves you the trouble of developing backend support for the applications you create. With the help of Firebase, our application will be able to perform data backup in real-time - of course, the necessary API calls are still indispensable.

Although AngularJS itself is already quite powerful, with the help of Firebase, we will be able to take our application results to the next level.

The text starts here, please note:

Before you start using AngularJS to create this simple small web application, you first need to download the angular-seed project. After the download is completed, you need to open the corresponding download directory and install the dependencies in it to run it. The specific code is as follows:

$ cd angular-seed 
$ npm install ## Install the dependencies
Copy after login

The next step is to use the following representative to start the node server:

$ npm start ## Start the server
Copy after login

After the node server is up and running, we need to open the browser And visit http://localhost:8000/app/index.html, the running default application will be displayed.

Next, visit the application directory under the angular-seed project folder, where the application code is saved.

As the core of the application, app.js will also be stored in the application folder. All application-level modules and routes within app.js need to be declared.

In addition, you will also find two views of angular-seed here, namely view 1 and view 2. They always exist in their default form. We need to delete these views in the application folder.

Now we have to create the application from scratch: you first need to open app.js and delete all existing code in it. Defining our application routes in app.js requires us to use ngRoute, one of the modules in AngularJS. By default app.js does not contain this module, so we need to manually inject it into the application to use it. You can use the following code to complete the addition of the AngularJS module:

angular.module('myApp', [ 
'ngRoute' 
])
Copy after login

The ngRoute module will bring an important component, namely $routeProvider, which can perfectly configure routing. We need to use the following code to inject $routeProvider into the configuration method of angular-module to complete the route definition:

'use strict'; 
angular.module('myApp', [ 
'ngRoute' 
]). 
config(['$routeProvider', function($routeProvider) { 
// Routes will be here 
}]);
Copy after login

After completing the above steps, we can now open index.html. Clear all the content in index.html, leaving only script references and divs.

Every time we make a routing change, we need to adjust the div content according to the above method. (If you want to learn more, I recommend the PHP Chinese website, which has the angularjs video tutorial you want to watch)

Now create symbols in the view:

We need to create a new folder in the app directory and name it home. In this folder, we create two additional folders, home.js and home.html. First open home.html and add the following code to it:

<!DOCTYPE html> 
<html ng-app="myApp"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<link rel="icon" href="http://www.php.cn/favicon.ico"> 
<title>AngularJS & Firebase Web App</title> 
<link href="http://www.php.cn/dist/css/bootstrap.min.css" rel="stylesheet"> 
<link href="http://www.php.cn/examples/signin/signin.css" rel="stylesheet"> 
<link href="justified-nav.css" rel="stylesheet"> 
</head> 
<body> 
<div> 
<div style="padding-bottom:0px;"> 
<h2>AngularJS & Firebase App!</h2> 
</div> 
<form role="form"> 
<input type="email" placeholder="Email address" required="" autofocus=""> 
<input type="password" placeholder="Password" required=""> 
<label> 
<a href="#"> Sign Up</> 
</label> 
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> 
</form> 
</div> 
</body>
</html>
Copy after login

在home.js当中,我们则需要创建一套路由机制以访问home视图。另外还需要为由home视图创建的$scope设置一套控制器。控制器永远负责控制与之对应的特定视图。具体代码如下所示:

use strict&#39;; 
angular.module(&#39;myApp.home&#39;, [&#39;ngRoute&#39;]) 
// Declared route 
.config([&#39;$routeProvider&#39;, function($routeProvider) { 
$routeProvider.when(&#39;/home&#39;, { 
templateUrl: &#39;home/home.html&#39;, 
controller: &#39;HomeCtrl&#39; 
}); 
}]) 
// Home controller 
.controller(&#39;HomeCtrl&#39;, [function() { 
}]);
Copy after login

现在应用程序已经准备就绪。打开app.js,而后将myApp.home home模块添加到该应用当中。利用$routeProvider.otherwise方法为我们的应用程序声明一套指向home视图的默认路由,具体代码如下所示:

&#39;use strict&#39;; 
angular.module(&#39;myApp&#39;, [ 
&#39;ngRoute&#39;, 
&#39;myApp.home&#39;           // Newly added home module 
]). 
config([&#39;$routeProvider&#39;, function($routeProvider) { 
// Set defualt view of our app to home 
$routeProvider.otherwise({ 
redirectTo: &#39;/home&#39; 
}); 
}]);
Copy after login

如果大家希望显示自己的home页面,则将home.js添加到该应用的主HTML模板文件当中。要完成这项操作,请打开index.html文件并湢以下代码:

<script src="home/home.js"></script>
Copy after login

现在一切工作已经完成,该应用随时准备加以运行了!要开始使用这款应用,我们需要重启服务器并将自己的浏览器指向http://localhost:8000/app/index.html以访问登入页面,在这里大家可以实现对该应用程序的访问。

如果大家需要使用Firebase(具体理由如前文所述),则需要首先创建一个Firebase账户。在账户创建完成后,我们将屏幕上所显示的已创建应用url添加进来,而后点击“管理该应用”。

创建自己的应用程序感觉不错吧?Angular.js能够为此类开发工作提供我们所需要的一切。而且只需几分钟,我们的这款简单小应用就已经正式上线啦!

好了,以上就是本篇关于angularjs开发web应用的全部内容了(想学更多就拉PHP中文网,AngularJS使用手册栏目学习),你学会了吗?没学会的继续读,知道学会为止。有问题的可以在下方留言提问。

【小编推荐】

angularjs的路由原理你知道吗?这里有angularjs路由的详细原理

angularjs应用场景有哪些?angularjs的应用场景介绍

The above is the detailed content of How to develop web applications with angularjs? Angularjs development web application examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!