Home > Article > Web Front-end > How to learn AngularJS? Angularjs learning tutorial keeps you from getting lost
This article mainly talks about the syntax introduction of angularjs, as well as the scope explanation of angularjs, and the explanation of angularjs controller and service. Next, let us look at this article together. Article Bar
Record unfamiliar angularjs syntax
##AngularJS Scope
$rootScope : 根作用域,所有 controller 都可以调用,类似于项目级别的全局变量 js赋值 : $rootScope.lastname = "Refsnes"; html调用 : $root.lastname$scope scope
之间无法互相访问
Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带。
Scope 是一个对象,有可用的方法和属性。
Scope 可应用在视图和控制器上
AngularJS controller
控制 AngularJS 应用程序的数据。 AngularJS 控制器是常规的 JavaScript 对象。 ng-controller ng-controller 指令定义了应用程序控制器 ng-controller="myCtrl" 属性是一个 AngularJS 指令。用于定义一个控制器。 myCtrl 函数是一个 JavaScript 函数。 JS引用<script src="personController.js"></script> 多个controller js文件中 定义一个app 之后可以定义多个controller HTML中也可以对应多个controller
var app = angular.module('myApp', []); app.controller('myCtrl1', function($scope) { $scope.firstName = "Johns"; $scope.lastName = "Doef"; }); app.controller('myCtrl2', function($scope) { $scope.firstName = "Tom"; $scope.lastName = "kkk"; });
AngularJS filter Filter
currency Format numbers into currency format
filter Select a subset from the array items.
lowercase Format strings to lowercase.
orderBy Arrange the array according to an expression
uppercase Format the string to uppercase
Filter input
The input filter can pass a pipe character ( |) and a filter added to the directive followed by a colon and a model name.
<p><input type="text" ng-model="test"></p><ul> <li ng-repeat="x in names | filter:test | orderBy:'country'"> {{ (x.name | uppercase) + ', ' + x.country }} </li></ul>Custom filter
<!DOCTYPE html><html><meta charset="utf-8"><script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script> <body> <p ng-app="myApp" ng-controller="myCtrl">姓名: {{ msg | reverse }}</p><script>var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.msg = "Runoob"; }); app.filter('reverse', function() { //可以注入依赖 return function(text) { return text.split("").reverse().join(""); } });</script> </body></html>
uppercase, lowercase case conversion (If you want to learn more, go to the PHP Chinese website
AngularJS Development ManualAngularJS Service## In #AngularJS you can create your own services or use built-in services. In AngularJS, a service is a function or object that can be used in your AngularJS application.
$location
##location.absUrl(); Built-in service, get the current The url address of the page
$http service
$http is the most commonly used service in AngularJS applications. The service sends a request to the server, and the application responds to the data sent by the server.
##interval service corresponds to the JS window.setInterval function. interval can be used to set a delay service that always runs.
Create a custom serviceTo use a custom service, you need to add it independently when defining the controller and set dependencies: After you create a custom service and connect it to your application, you can use it in controllers, directives, filters, or other services.
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script></head><body><p ng-app="myApp" ng-controller="myCtrl"><p>255 的16进制是:</p><h1>{{hex}}</h1></p><p>自定义服务,用于转换16进制数:</p><script>var app = angular.module('myApp', []); app.service('hexafy', function() { this.myFunc = function (x) { return x.toString(16); } }); app.controller('myCtrl', function($scope, hexafy) { $scope.hex = hexafy.myFunc(255); });</script></body></html>
#http.get -http.post -http.delete
-http.patch
Simple get/post request
// 简单的 GET 请求,可以改为 POST$http({ method: 'GET', url: '/someUrl'}).then(function successCallback(response) { // 请求成功执行代码 }, function errorCallback(response) { // 请求失败执行代码});$http.get('/someUrl', config).then(successCallback, errorCallback);$http.post('/someUrl', data, config).then(successCallback, errorCallback);AngularJS Select(select box)AngularJS You can create a drop-down list of options using an array or object.
ng-option directive to create a drop-down list, the list items are output in a loop through objects and arrays
scope.cars.car02; //设置第2个为初始值;
The above is the detailed content of How to learn AngularJS? Angularjs learning tutorial keeps you from getting lost. For more information, please follow other related articles on the PHP Chinese website!