search
HomeWeb Front-endJS TutorialDevelop a large-scale single-page application (SPA) using AngularJS - Technical Translation

Introduction

(SPA) What is contained in a name like this? If you are a fan of the classic Seinfeld TV show, then you must know the name Donna Chang. Jerry met with Donna. Donna was actually not Chinese, but because she was talking about her inherent impression of China, such as her interest in acupuncture, and accidentally pronounced a word with a Chinese accent, she shortened the last name of her name to Chang Donna talked to George's mother on the phone and gave her some advice (by quoting Confucius). When George introduced Donna to his parents, George's mother realized that Donna was not Chinese, so she did not accept Donna's suggestion.

Single Page Reference (SPA), is defined as an application that aims to provide a desktop-like application A smooth user experience for a single web page application, or website. In a SPA, all required code – HTML, JavaScript, and CSS – is fetched when the single page loads, or related resources are dynamically loaded and Added to pages on demand, often in response to user actions. Although modern web technologies (such as those introduced in HTML5) provide the ability for independent logical pages in an application to perceive and navigate each other , the page does not reload any endpoints in the process, or transfer control to another page. Interaction with single-page applications is often designed to dynamically interact with the web server located in the background.


So take this How does this technology compare to ASP.NET's Master Pages? It's true that ASP.NET's Master Pages allow you to create a consistent layout for the pages in your application. A single master page can define the appearance and standard actions you want to apply to all pages (or groups of pages) in the entire application. You can then create separate pages for the content you want to display. . When a user initiates a request for a content page, they will mix the layout from the master page with the content from the content page to produce the output.

When you dig into SPA and ASP.NET master pages to achieve this When it comes to the differences between the two, you begin to realize that they are more similar than different - that is, the SPA can be regarded as a simple shell page that holds the content page, like a master page, it's just that the shell page in the SPA cannot be reloaded and executed on every new page request like the master page.


Maybe "Single Page Application" is an unlucky name (like Donna`Cheng) ), leading you to believe that this technology is not suitable for developing web applications that need to be expanded to the enterprise level and may include hundreds of pages and thousands of users.

The goal of this article is to develop an enterprise-level application with hundreds of pages of content based on a single-page application, including authentication, authorization, session state and other functions, which can support thousands of users.



AngularJS - Overview


The examples in this article include functions such as creating/new user accounts, creating/updating customers and products. Furthermore, it allows users to perform queries, create and follow up sales orders on all information. In order to implement these functions, this sample will be developed based on AngularJS. AngularJS is an open source web application framework maintained by developers from Google and the AngularJS community.

AngularJS can create single-page applications on the client side with just HTML, CSS and JavaScript. Its goal is to make development and testing easier and enhance the performance of MVC web applications.


This library reads other custom tag attributes contained in HTML; then obeys the instructions of this custom attribute and combines the I/O of the page into a module with standard JavaScript variable generation. The values ​​of these JavaScript standard variables can be set manually or obtained from a static or dynamic JSON data source.



Getting Started with AngularJS - Shell Pages, Modules and Routes


One of the first things you need to do is download the AngularJS framework into your project, you can download it from http ://www.php.cn/ Get the framework. The sample program in this article was developed using MS Visual Studio Web Express 2013 Edition, so I used the following command to install AngularJS from a Nuget package:

Install-Package AngularJS - Version 1.2.21

on the Nuget Package Management Console. To keep it simple and flexible, I created an empty Visual Studio web application project and selected the Microsoft Web API 2 library into the core references. This The application will use the Web API 2 library to implement server-side requests for RESTful APIs.


Now when you want to create a SPA application using AngularJS, the first two things to do are to set up a shell page and a routing table for getting the content page. At the beginning, the shell page only needs a team of AngularJS JavaScript libraries Reference, and an ng-view, to tell AngularJS where the content page needs to be rendered in the shell page.

<!DOCTYPE html>
<html lang="en">
<head>
<title>AngularJS Shell Page example</title>
</head>
<body> 
<p>
<ul>
<li><a href="#Customers/AddNewCustomer">Add New Customer</a></li>
<li><a href="#Customers/CustomerInquiry">Show Customers</a></li>
</ul>
</p>
<!-- ng-view directive to tell AngularJS where to inject content pages -->
<p ng-view></p>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>


In the shell page example above, several links are mapped to AngularJS routes. The ng-view directive on the p tag is a directive that can include the rendered content page of the selected route into the shell page to supplement AngularJS's $route service. Every time the current route changes, the included view will also be based on $ The configuration of the route service changes accordingly. For example, when the user selects the "Add New Customer" link, AngularJS will render the content for adding a new customer in the p where the ng-view is located. The rendered content is an HTML fragment .


The next app.js file is also referenced by the shell page. The JavaScript in this file will create the AngularJS module for the application. Additionally, all routing configuration for the application will be defined in this file. You can think of an AngularJS module as a container that encapsulates different parts of your application. Most applications will have a main method that initializes and connects different parts of the application. AngularJS applications, on the other hand, do not have a main method, instead letting modules declaratively specify how the application is started and configured. The sample application for this article will only have one AngularJS module, although there are several distinct parts of the application (customers, Products, Orders and Users).

Now, the main purpose of app.js is to set up AngularJS routing as shown below. AngularJS's $routeProvider service will accept the when() method, which will match a pattern for a Uri. When a match is found, the HTML content of the independent page will be loaded into the shell page along with the controller file of the related content. . The controller file is simply a JavaScript file that will get a reference with the content of a specific route request.

//Define an angular module for our app
var sampleApp = angular.module(&apos;sampleApp&apos;, []);
//Define Routing for the application
sampleApp.config([&apos;$routeProvider&apos;,
    function($routeProvider) {
        $routeProvider.
            when(&apos;/Customers/AddNewCustomer&apos;, {
                templateUrl: &apos;Customers/AddNewCustomer.html&apos;,
                controller: &apos;AddNewCustomerController&apos;
            }).
            when(&apos;/Customers/CustomerInquiry&apos;, {
                templateUrl: &apos;Customers/CustomerInquiry.html&apos;,
                controller: &apos;CustomerInquiryController&apos;
            }).
            otherwise({
                redirectTo: &apos;/Customers/AddNewCustomer&apos;
            });
}]);


AngularJS controller


AngularJS controller is nothing more than a native JavaScript functions are just bound to a specific scope. Controllers are used to add logic to your views. Views are HTML pages. These pages are just for simple data display. We will use two-way data binding to bind data to these HTML pages. It is basically the responsibility of the controller to bond the model (that is, data) with the data.

<p ng-controller="customerController">
<input ng-model="FirstName" type="text" style="width: 300px" />
<input ng-model="LastName" type="text" style="width: 300px" />       
<p>
<button class="btn btn-primary btn-large" ng-click="createCustomer()"/>Create</button>



For the AddCustomer template above, the ng-controller directive will reference the JavaScript function customerController, which will perform all data binding and JavaScript functions for the view.

function customerController($scope) 
{
    $scope.FirstName = "William";
    $scope.LastName = "Gates"; 

    $scope.createCustomer = function () {          
        var customer = $scope.createCustomerObject();
        customerService.createCustomer(customer, 
                        $scope.createCustomerCompleted, 
                        $scope.createCustomerError);
    }
}


Out of the box Ready to Use - Scalability Issues


As I was developing this powerhouse program for this article, the first two scalability issues became apparent when applying a single page application. In fact, out of the box, AngularJS requires that all JavaScript files and controllers in the application's shell page be introduced and downloaded at startup with the application. For a large application, there may be hundreds of them. JavaScript file, so the situation doesn't look very ideal. Another problem I encountered was AngularJS's routing table. All examples I've found have all routes hardcoded for everything. And what I want is not a solution that contains hundreds of routing records in the routing table.

Statement
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
JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.