Home > Article > Web Front-end > What should you know when you first get to know angularjs? A must-learn tutorial for getting started with angularjs in 2018 (with examples)
This article introduces the basic understanding of angularjs, so that students who want to learn angularjs can have a better introductory course. Let’s take a look at this article now.
AngularJs is a front-end framework, a library written in JavaScript. Users can use this framework to perform two-way binding of data in the front end to realize MVC data display on the front end.
When using the AngularJs framework, you only need to introduce the AngularJs file into the front-end html. Very easy to use.
Before explaining the basic instructions, let’s introduce some basic concepts. After the user introduces the AngularJs file into the page, he can create the WebApp and controller. In AngularJs, each page is regarded as an independent WebApp. Each WebApp can include multiple independent processing parts, which we call Controllers. Each Controller has its own independent processing context and logic. AngularJs delimits the scope of this WebApp through the instruction ng-app. Usually, this ng-app can be written directly on the html tag or on an element. For example:
html:<p ng-app="myApp"> <p ng-controller="myController"> <h3>{{data}}</h3> </p></p>
Initialize the application: The ng-app directive tells AngularJS that the
element is the "owner" of the AngularJS application.
Bind data: The ng-model directive binds the value of the input field to the application variable name. ---Bind to the property. Also available: Provide type validation for application data (number, email, required). Provide status (invalid, dirty, touched, error) for application data. Provides CSS classes for HTML elements. Bind HTML elements to HTML forms.
Bind data in html: The ng-bind directive binds the application variable name to the innerHTML of a paragraph. Equivalent to writing {{variable name}} in html——————Bind to text
Initialize data: The ng-init directive initializes AngularJS application variables.
Clone data: The ng-repeat directive clones the HTML element once for each item in the collection (array). eg:ng-repeat=”x in arrays”
Custom instructions: Add custom instructions by using the .directive function. To call a custom directive, the custom directive name needs to be added to the HTML element. Use camelCase to name a directive, runoobDirective, but you need to separate it with - when using it, runoob-directive.
eg:<body ng-app="myApp"> <runoob-directive></runoob-directive> <script> var app = angular.module("myApp", []); app.directive("runoobDirective", function() { return { //restrict 默认值为 EA, 即可以通过元素名和属性名来调用指令。 restrict : "A",//限制使用--E 作为元素名使用。A 作为属性使用。C 作为类名使用。M 作为注释使用 template : "<h1>自定义指令!</h1>" }; }); </script></body>
Controller: The ng-controller directive defines the application controller.
Filter: Filters can be added to expressions via a pipe character (|) and a filter.
service: In AngularJS you can create your own services or use built-in services. A service is a function or object that can be used in your AngularJS application. AngularJS has more than 30 built-in services. Here are some commonly used built-in services. (If you want to learn more, go to the PHP Chinese website AngularJS Development Manual to learn)
1,$location:类似 window.location 对象,但 window.location 对象在 AngularJS 应用中有一定的局限性。2,$http:AngularJS 应用中最常用的服务。 服务向服务器发送请求,应用响应服务器传送过来的数据。3,$timeout:AngularJS $timeout 服务对应了 JS window.setTimeout 函数。4,$interval:AngularJS $timeout 服务对应了 JS window.setInterval 函数。5,用户可以根据自己的需求来创建自定义服务。
http: A core service in angularJS, used to read remote servers The data. Note that the success and error methods of $http have been deprecated in v1.5. Use the then method instead
格式:/ 简单的 GET 请求,可以改为 POST$http({ method: 'GET', url: '/someUrl'}).then(function successCallback(response) { // 请求成功执行代码 }, function errorCallback(response) { // 请求失败执行代码});
Select box (select): AngularJS can use an array or object to create a drop-down list of options.
1,使用ng-options来创建一个对象<p ng-app="myApp" ng-controller="myCtrl"> <select ng-init="selectedName = names[0]" ng-model="selectedName" ng-options="x for x in names"> </select></p><script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.names = ["Google", "Runoob", "Taobao"]; });</script>2,使用ng-repeat来创建对象,<select ng-model="selectedSite"> <option ng-repeat="x in names">{{x}}</option></select>3,区别:ng-options,选择的值是一个对象:也就是当select获取值selectedSite时候,得到的是一个对象。ng-repeat,选择的值是一个字符串:也就是当select获取值selectedSite时候,得到的是一个字符串
Table: The ng-repeat directive in AngularJs can display the table perfectly. You can also use filters when displaying the table.
<p ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names | orderBy : 'Name'"> <td>{{ $index + 1 }}</td>//显示序号 <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table></p><script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("/try/angularjs/data/Customers_JSON.php") .then(function (result) { $scope.names = result.data.records; }); });</script>
HTML DOM: AngularJS 为 HTML DOM 元素的属性提供了绑定应用数据的指令。
1,ng-disabled 指令直接绑定应用程序数据到 HTML 的 disabled 属性。
2,ng-show 指令隐藏或显示一个 HTML 元素。ng-show 指令根据 value 的值来显示(隐藏)HTML 元素。你可以使用表达式来计算布尔值( true 或 false):
3,ng-hide 指令用于隐藏或显示 HTML 元素。注意在页面中如果使用input type=hidden来做表单传值的时候,是不支持的。
4,ng-click 指令定义了 AngularJS 点击事件。
模块: 模块定义了一个应用程序。模块是应用程序中不同部分的容器。模块是应用控制器的容器。控制器通常属于一个模块。
注意: 对于 HTML 应用程序,通常建议把所有的脚本都放置在 元素的最底部。这会提高网页加载速度,因为 HTML 加载不受制于脚本加载。在我们的多个 AngularJS 实例中,您将看到 AngularJS 库是在文档的 区域被加载。在我们的实例中,AngularJS 在 元素中被加载,因为对 angular.module 的调用只能在库加载完成后才能进行。另一个解决方案是在 元素中加载 AngularJS 库,但是必须放置在您的 AngularJS 脚本前面:
表单: AngularJS 表单是输入控件的集合。
1,Input 控件使用 ng-model 指令来实现数据绑定。
2,checkbox 的值为 true 或 false,可以使用 ng-model 指令绑定,它的值可以用于应用中。举个例子:
<body ng-app="myApp" ng-controller="person"> <form ng-submit="submit()" name="myForm"> <p>职业: <input type="checkbox" ng-model="user.jobs.engineer">工程师 <input type="checkbox" ng-model="user.jobs.designer">设计师 <input type="checkbox" ng-model="user.jobs.teacher">教师 </p> <input type="submit" value="提交"> <input type="button" value="选中教师" ng-click="select_teacher()"> </form></body><script> var app = angular.module("myApp", []); app.controller("person", function($scope) { $scope.user = { jobs: { engineer: true, designer: false, teacher: false } }; $scope.submit = function() { alert(JSON.stringify($scope.user)); }; $scope.select_teacher = function() { $scope.user.jobs.teacher = true; } });</script>
3,radio:我们可以使用 ng-model 来绑定单选按钮到你的应用中。单选框使用同一个 ng-model ,可以有不同的值,但只有被选中的单选按钮的值会被使用。
4,下拉菜单上面已经写过。在此不再复述。
输入验证: AngularJS 表单和控件可以提供验证功能,并对用户输入的非法数据进行警告。
<form ng-app="myApp" ng-controller="validateCtrl" name="myForm" novalidate> <p>用户名:<br> <input type="text" name="user" ng-model="user" required> <span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid"> <span ng-show="myForm.user.$error.required">用户名是必须的。</span> </span> </p> <p> <input type="submit" ng-disabled="myForm.user.$dirty && myForm.user.$invalid </p> </form> <script> var app = angular.module('myApp', []); app.controller('validateCtrl', function($scope) { $scope.user = 'John Doe'; }); </script>
注意: 我们使用了 ng-show指令, color:red 在邮件的
invalid 都为 true 时才显示。
属性 描述$dirty 表单有填写记录$valid 字段内容合法的$invalid 字段内容是非法的$pristine 表单没有填写记录
API: AngularJS 全局 API 用于执行常见任务的 JavaScript 函数集合,如:比较对象,迭代对象,转换对象,全局 API 函数使用 angular 对象进行访问。
以下列出了一些通用的 API 函数:
angular.lowercase() 转换字符串为小写 angular.uppercase() 转换字符串为大写 angular.isString() 判断给定的对象是否为字符串,如果是返回 true。 angular.isNumber() 判断给定的对象是否为数字,如果是返回 true。
注意点:
1,HTML5 允许扩展的(自制的)属性,以 data- 开头。AngularJS 属性以 ng- 开头,但是您可以使用 data-ng- 来让网页对 HTML5 有效。
2,类似于 JavaScript 表达式,AngularJS 表达式可以包含字母,操作符,变量。
与 JavaScript 表达式不同,AngularJS 表达式可以写在 HTML 中。AngularJS 表达式不支持条件判断,循环及异常。AngularJS 表达式支持过滤器。
好了,本篇文章到这就结束了(想看更多就到PHP中文网AngularJS使用手册中学习),有问题的可以在下方留言提问。
The above is the detailed content of What should you know when you first get to know angularjs? A must-learn tutorial for getting started with angularjs in 2018 (with examples). For more information, please follow other related articles on the PHP Chinese website!