Table of Contents
AngularJS controller
Home Web Front-end JS Tutorial Develop a large-scale single-page application (SPA) using AngularJS - Technical Translation

Develop a large-scale single-page application (SPA) using AngularJS - Technical Translation

Dec 05, 2016 pm 01:44 PM
translate

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<!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>

Copy after login


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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

//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;

            });

}]);

Copy after login


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.

1

2

3

4

5

<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>

Copy after login



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.

1

2

3

4

5

6

7

8

9

10

11

12

function customerController($scope)

{

    $scope.FirstName = "William";

    $scope.LastName = "Gates";

 

    $scope.createCustomer = function () {         

        var customer = $scope.createCustomerObject();

        customerService.createCustomer(customer,

                        $scope.createCustomerCompleted,

                        $scope.createCustomerError);

    }

}

Copy after login


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 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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if the translation web page that comes with the Edge browser is missing? What should I do if the translation web page that comes with the Edge browser is missing? Mar 14, 2024 pm 08:50 PM

The edge browser comes with a translation function that allows users to translate anytime and anywhere, which brings great convenience to users. However, many users say that the built-in translation webpage is missing. Then the edge browser automatically What should I do if the translation page I brought is missing? Let this site introduce how to restore the translated web page that comes with the Edge browser if it is missing. How to restore the translation webpage that comes with the Edge browser is missing 1. Check whether the translation function is enabled: In the Edge browser, click the three dots icon in the upper right corner, and then select the "Settings" option. On the left side of the settings page, select the Language option. Make sure "Translate&rd"

Don't worry about watching movies without subtitles! Xiaomi announces the launch of Xiaoai Translation real-time subtitles for Japanese and Korean translation Don't worry about watching movies without subtitles! Xiaomi announces the launch of Xiaoai Translation real-time subtitles for Japanese and Korean translation Jul 22, 2024 pm 02:11 PM

According to news on July 22, today, the official Weibo of Xiaomi ThePaper OS announced that Xiaoai Translation has been upgraded. Real-time subtitles have been added to Japanese and Korean translations, and subtitle-free videos and live conferences can be transcribed and translated in real time. Face-to-face simultaneous interpretation supports translation into 12 languages, including Chinese, English, Japanese, Korean, Russian, Portuguese, Spanish, Italian, French, German, Indonesian, and Hindi. The above functions currently only support the following three new phones: Xiaomi MIX Fold 4 Xiaomi MIX Flip Redmi K70 Extreme Edition It is reported that in 2021, Xiao Ai’s AI subtitles will be added to Japanese and Korean translations. AI subtitles use Xiaomi’s self-developed simultaneous interpretation technology to provide a faster, more stable and accurate subtitle reading experience. 1. According to the official statement, Xiaoai Translator can not only be used in audio and video venues

How to translate Sogou browser How to translate Sogou browser Feb 01, 2024 am 11:09 AM

How does Sogou browser translate? When we usually use Sogou browser to check information, we will encounter some websites that are all in English. Because we can’t understand English, it is very difficult to browse the website. This is also very inconvenient. It doesn’t matter if you encounter this situation! Sogou Browser has a built-in translation button. With just one click, Sogou Browser will automatically translate the entire webpage for you? If you don’t know how to operate it, the editor has compiled the specific steps on how to translate it on Sogou Browser. If you don’t know how, follow me and read on! How to translate Sogou Browser 1. Open Sogou Browser, click the translation icon in the upper right corner 2. Select the type of translation text, and then enter the text that needs to be translated 3. Sogou Browser will automatically translate the text. At this point, the above Sogou Browsing operation is completed. How to translate all contents

How to solve the problem that Google Chrome's built-in translation fails? How to solve the problem that Google Chrome's built-in translation fails? Mar 13, 2024 pm 08:46 PM

Browsers generally have built-in translation functions, so you don’t have to worry about not being able to understand when browsing foreign language websites! Google Chrome is no exception, but some users find that when they open the translation function of Google Chrome, there is no response or failure. What should they do? You can try the latest solution I found. Operation tutorial: Click the three dots in the upper right corner and click Settings. Click Add Language, add English and Chinese, and make the following settings for them. The English setting asks whether to translate web pages in this language. The Chinese setting displays web pages in this language, and Chinese must be moved to the top before it can be set as the default language. If you open the webpage and no translation option pops up, right-click and select Translate Chinese, OK.

Building a real-time translation tool based on JavaScript Building a real-time translation tool based on JavaScript Aug 09, 2023 pm 07:22 PM

Building a real-time translation tool based on JavaScript Introduction With the growing demand for globalization and the frequent occurrence of cross-border exchanges and exchanges, real-time translation tools have become a very important application. We can leverage JavaScript and some existing APIs to build a simple but useful real-time translation tool. This article will introduce how to implement this function based on JavaScript, with code examples. Implementation Steps Step 1: Create HTML Structure First, we need to create a simple HTML

Why can't Google Chrome translate Chinese? Why can't Google Chrome translate Chinese? Mar 11, 2024 pm 04:04 PM

Why can't Google Chrome translate Chinese? As we all know, Google Chrome is one of the browsers with built-in translation. When you browse pages written in other countries in this browser, the browser will automatically translate the page into Chinese. Recently, some users have said that they Chinese translation cannot be performed. At this time, we need to fix it in the settings. Next, the editor will bring you the solution to the problem that Google Chrome cannot translate into Chinese. Friends who are interested should come and take a look. Google Chrome cannot translate Chinese solutions 1. Modify the local hosts file. Hosts is a system file without an extension. It can be opened with tools such as Notepad. Its main function is to define the mapping relationship between IP addresses and host names. It is a mapping IP address

How to solve the problem that Sogou Browser cannot translate web pages How to solve the problem that Sogou Browser cannot translate web pages Jan 29, 2024 pm 09:18 PM

What should I do if Sogou Browser cannot translate this webpage? Sogou Browser is a very easy-to-use multi-functional browser. Its web page translation function is very powerful and can help us solve most of the troubles in study and work. However, some friends reported that Sogou Browser has a problem that it cannot translate this web page. This may be caused by improper operation. It can be solved by operating the translation function correctly. Below, the editor will bring you the problem that Sogou Browser cannot translate. Translate this page solution. Sogou Browser cannot translate this webpage Solution Method 1: 1. Download and install Sogou Browser 2. Open Sogou Browser 3. Open any English website 4. After the website is opened, click the translation icon in the upper right corner 5. Select Translate text type and click Translate current web page 6

iOS 17.2: How to use your iPhone's action button to translate speech iOS 17.2: How to use your iPhone's action button to translate speech Dec 15, 2023 pm 11:21 PM

In iOS 17.2, overcome communication barriers with new custom translation options for iPhone action buttons. Read on to learn how to use it. If you have an iPhone with an action button, like the iPhone 15 Pro, Apple's iOS 17.2 software update brings new translation options to the button, allowing you to translate live conversations into multiple languages. According to Apple, the translations are not only accurate, but also context-aware, ensuring nuances and spoken language are captured effectively. This feature should be a boon for travelers, students, and anyone learning a language. Before using the translation feature, be sure to select the language you want to translate into. You can do this through Apple's built-in Translate app

See all articles