SoFunction
Updated on 2025-04-08

The first article of AngularJS learning AngularJS basic knowledge

The first article of AngularJS learning is to learn about instructions, filters and other related content.

instruction

AngularJS directive is an extended HTML attribute with a prefix ng-

1、 ng-app:

Defines the root element of the AngularJS application;
The ng-app directive will automatically boot (auto-initialize) the application when the web page is loaded;

<div ng-app="Demo"></div>

2、 ng-init:

Defines the initial value for the AngularJS application;
Normally, we use a controller or module instead;

&lt;div ng-app="Demo" ng-init="firstName='John'"&gt;
 &lt;p&gt;My name is:{{ firstName }}&lt;/p&gt;
&lt;/div&gt;

3、 ng-model:

Bind HTML elements to application data
Also:
Provide type verification (number, email, required) for application data;
Provide status (invalid, dirty, touched, error) for application data;
Provide CSS classes for HTML elements;
Bind HTML elements to HTML form;

&lt;div ng-app="Demo" ng-init="firstName='John'"&gt;
 &lt;p&gt;Name:&lt;input type="text" ng-model="firstName"&gt;&lt;/p&gt;
 &lt;p&gt;My name is:{{ firstName }}&lt;/p&gt;
&lt;/div&gt;

4、ng-repeat:HTML elements are cloned once for each item in the collection (in an array).

<div ng-app="Demo" ng-init="names=[
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]">
 <ul>
   <li ng-repeat="x in names">
    {{  + ', ' +  }}
   </li>
 </ul>
</div> 

5、ng-controller:Add controllers for the application. Please learn from the following example:

&lt;div ng-app="Demo"&gt;
 &lt;h1 ng-controller="DemoCtrl"&gt;{{name}}&lt;/h1&gt;
 &lt;h1 ng-controller="DemoCtrl2"&gt;{{lastName}}&lt;/h1&gt;
&lt;/div&gt;
&lt;script&gt;
 // $scope represents the action area, pointing to the current controller // Each application has a $rootScope, which can be used in all HTML elements contained in the ng-app directive.  Values ​​defined with rootscope can be used in each controller. var app = ('Demo', []);
 ('DemoCtrl', function($scope, $rootScope) {
  $ = "Volvo";
  $ = "Tom";
 });
&lt;/script&gt;
&lt;div ng-app="Demo" ng-controller="personCtrl"&gt;
 name: &lt;input type="text" ng-model="firstName"&gt;
 &lt;br&gt;
 surname: &lt;input type="text" ng-model="lastName"&gt;
 &lt;br&gt;
 surnamename: {{fullName()}}
&lt;/div&gt;
&lt;script&gt;
 var app = ('Demo', []);
 ('personCtrl', function($scope) {
  $ = "John";
  $ = "Doe";
  $ = function() {
   return $ + " " + $;
  }
 });
&lt;/script&gt;

6、ng-options:Create a drop-down list, and the list items are output loop through objects and arrays.

<div ng-app="Demo" ng-controller="DemoCtrl">
 <select ng-model="selectedName" ng-options="x for x in names">
 </select>
</div>
<script>
var app = ('myApp', []);
('myCtrl', function($scope) {
 $ = ["Google", "W3Cschool", "Taobao"];
});
</script>

7、ng-disabled:Directives bind application data directly to the disabled attribute of HTML.

&lt;div ng-app="" ng-init="mySwitch=true"&gt;
 &lt;button ng-disabled="mySwitch"&gt;Click me!&lt;/button&gt;
 &lt;input type="checkbox" ng-model="mySwitch"/&gt;Button
 {{ mySwitch }}
&lt;/div&gt; 

8、ng-show:Directive hides or displays an HTML element.

&lt;div ng-app=""&gt;
 &lt;p ng-show="true"&gt;I'm visible。&lt;/p&gt;
 &lt;p ng-show="false"&gt;I'm invisible。&lt;/p&gt;
&lt;/div&gt; 

9、ng-click:The directive defines an AngularJS click event.

&lt;div ng-app="Demo" ng-controller="myController"&gt;
 &lt;button ng-click="count = count + 1"&gt;Click me!&lt;/button&gt;
 &lt;p&gt;{{ count }}&lt;/p&gt;
&lt;/div&gt;

10、ng-include:Use the ng-include directive to include HTML content.

Filter

Add to expressions and directives using a pipe character (|)
Common expressions:
currency: Format numbers into currency format;
filter: Select a subset from the array item;
lowercase: formatted strings in lowercase;
orderBy: arrange arrays according to a certain expression;
uppercase: formatted string in uppercase;

&lt;div ng-app="Demo" ng-controller="DemoCtrl"&gt;
 &lt;p&gt;Name is {{ lastName | uppercase }}&lt;/p&gt;
&lt;/div&gt;

<div ng-app="Demo" ng-controller="DemoCtrl">
 <ul>
 <li ng-repeat="x in names | orderBy:'country'">
  {{  + ', ' +  }}
 </li>
 </ul>
</div>

Serve

In AngularJS, a service is a function or object that can be used in your AngularJS application;
In AngularJS you can create your own services or use built-in services;
AngularJS has more than 30 built-in services;
Custom Services

('hexafy', function() {
  = function (x) {
 return (16);
 }
});
var app = ('Demo', []);
('customersCtrl', function($scope, $location) {
 $ = $();
}); 

Commonly used built-in services

1. $http: is a core service in AngularJS. The service sends a request to the server, and the application responds to the data transmitted by the server;

var app = ('Demo', []);
('DemoCtrl', function($scope, $http) {
 $http({
 url:'',
 method:'GET',
 params:{
  'username':'tan'
 }
 }).success(function(data,header,config,status){
 //Response successfully }).error(function(data,header,config,status){
 //Processing response failed });
});


2. $location: The service corresponds to the function.

3. $timeout: The service corresponds to the function.

4. $interval: The service corresponds to the function.

5. $rootScope: It can be used in all HTML elements contained in the ng-app directive. Values ​​defined with rootscope can be used in each controller.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.