Detailed explanation of how to use AngularJS to obtain json data
This article mainly introduces the method of AngularJS to obtain json data, and analyzes the detailed steps and operations of AngularJS to obtain json data in combination with examples. Skills and related Notes, friends in need can refer to the following
The example of this article describes the method of obtaining json data in AngularJS. I share it with you for your reference. The details are as follows:
After studying AngularJS for so many days, today I want to share with you a simple Demo from a practical perspective-user query system, to Consolidate previously learned knowledge. Functional requirements need to meet two points: 1. Query all user information and display it on the front end. 2. Query user information based on ID and display it on the front end. Ok, the requirements are very simple, then we start to implement the functional requirements.
CodeFramework
The front-end code usually contains three parts: html, css, and JavaScript, we use html to write the view file, css to control the view style, and JS to implement the controller code. The focus of this article is on the review and learning of AngularJS. You can use a simple html view and will not involve writing fancy CSS code. The fileDirectory structure of the code in this example is very simple, as shown in the figure below, which is divided into two simple directories. UserMgt is the package name of the entire Demo, and the JS directory is used to store third-party js code such as angular. js, controller is used to store our controller code, the tml directory stores html front-end files, and conf is used to store configuration files.
----------UserMgt
-------------JS
-------------controller
-------------tml
-------------conf
Code
In this example, we introduce the angular.js and angular-route.js v1.2.20 files and place them in our JS directory. The route provided by angularJS itself is not convenient enough to use. We use the third-party angular-route framework for routing allocation. First we need to write our front-end display interface.
1. index.html, the code is as follows
<!DOCTYPE html> <!--定义AngularJS app--> <html ng-app="UserMgt"> <head> <meta charset="utf-8"/> <title>user mgt demo </title> </head> <body> <h1>用户管理Demo</h1> <!--使用ng-show,表明我们使用路由控制来管理页面之间的跳转 --> <p ng-view> loading... </p><!--视图模板容器--> <!--引入ng-app所需的js文件--> <script type="text/javascript" src="../js/angular.js"></script> <script type="text/javascript" src="../js/angular-route.js"></script> <script type="text/javascript" src="../js/controller/mgt_controller.js"></script> </body> </html>
2.detail.html, used to display a user’s data information, the code is as follows
<table border="1"> <tr> <td>用户名</td> <!--使用ng-model绑定item对象的username属性--> <td><input type="text" ng-model="item.username"/></td> </tr> <tr> <td>男</td> <!--使用ng-model绑定item对象的gender属性--> <td><input type="text" ng-model="item.gender"/></td> </tr> <tr>` <td>邮箱</td> <!--使用ng-model绑定item对象的email属性--> <td><input type="text" ng-model="item.email"/></td> </tr> <tr> </tr> </table>
3 . list.html is used to display all data. The code is very simple as shown below
<table border="1"> <tr> <!--设置表头--> <td>用户名</td> <td>性别</td> <td>邮箱</td> </tr> <!--使用ng-repeat,遍历所有的user--> <tr ng-repeat="user in users"> <td>{{user.username}}</td> <td>{{user.gender}}</td> <td>{{user.email}}</td> </tr> </table>
4. mgt_controller.js
<!--定义UserMgt Ajs模块,模块依赖ngRoute--> var umService = angular.module('UserMgt', ['ngRoute']); <!--路由定义--> umService.config( function ($routeProvider) { $routeProvider <!--项目打开默认调到list.html页面,绑定ListController进行相应的控制--> .when('/', { controller: ListController, templateUrl: '../tml/list.html' }) <!--定义访问url--> .when('/get/:id', { <!--定义绑定的控制器--> controller: GetController, <!--定义跳转的页面--> templateUrl: "../tml/detail.html" }) .otherwise({ <!--其他情况,指定url跳转--> redirectTo: '/' }); } ) <!--ListController定义--> function ListController($scope, $http) { <!--获取本地json资源文件--> $http.get('../conf/user.json').success(function (data) { <!--浏览器console端口打印读取的数据--> console.log(data); $scope.users = data; }); } <!--GetController控制器定义--> function GetController($scope, $http, $routeParams) { var id = $routeParams.id; <!--获取本地json资源文件--> $http.get('../conf/user.json').success(function (data) { console.log(data); $scope.item = data[id]; }); }
5. Stored in json in user.json The following data:
[ { "id": 1, "username": "situ", "gender": "男", "email": "gao_st@126.com" }, { "id": 2, "username": "wb", "gender": "女", "email": "wb@126.com" }, { "id": 3, "username": "lml", "gender": "男", "email": "lml@126.com" }, { "id": 4, "username": "wjd", "gender": "女", "email": "wjd@126.com" }, { "id": 5, "username": "lyl", "gender": "男", "email": "lyl@126.com" }, { "id": 6, "username": "wjh", "gender": "女", "email": "wjh@126.com" } ]
Result
1. Display all user information
2. Obtain a certain user information
The above is the detailed content of Detailed explanation of how to use AngularJS to obtain json data. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The combination of golangWebSocket and JSON: realizing data transmission and parsing In modern Web development, real-time data transmission is becoming more and more important. WebSocket is a protocol used to achieve two-way communication. Unlike the traditional HTTP request-response model, WebSocket allows the server to actively push data to the client. JSON (JavaScriptObjectNotation) is a lightweight format for data exchange that is concise and easy to read.

The Gson@Expose annotation can be used to mark whether a field is exposed (contained or not) for serialization or deserialization. The @Expose annotation can take two parameters, each parameter is a boolean value and can take the value true or false. In order for GSON to react to the @Expose annotation, we have to create a Gson instance using the GsonBuilder class and need to call the excludeFieldsWithoutExposeAnnotation() method, which configures Gson to exclude all fields without Expose annotation from serialization or deserialization. Syntax publicGsonBuilderexclud

MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string. When writing programs in Golang, we often need to convert the structure into a JSON string. In this process, the json.MarshalIndent function can help us. Implement formatted output. Below we will explain in detail how to use this function and provide specific code examples. First, let's create a structure containing some data. The following is an indication

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Quick Start: Pandas method of reading JSON files, specific code examples are required Introduction: In the field of data analysis and data science, Pandas is one of the important Python libraries. It provides rich functions and flexible data structures, and can easily process and analyze various data. In practical applications, we often encounter situations where we need to read JSON files. This article will introduce how to use Pandas to read JSON files, and attach specific code examples. 1. Installation of Pandas

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic
