Einführung in AngularJS

AngularJS 是一个 JavaScript 框架。它可以使用一个<script>标签添加到 HTML 页面中。

AngularJS 通过指令扩展了 HTML 属性,并使用表达式将数据绑定到 HTML。

AngularJS 是一个 JavaScript 框架

AngularJS 是用 JavaScript 编写的 JavaScript 框架。

AngularJS 以 JavaScript 文件的形式分发,并且可以使用 script 标签添加到网页中:

<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>

AngularJS 扩展了 HTML

AngularJS 使用 ng-指令扩展了 HTML。

ng-app 指令定义了一个 AngularJS 应用程序。

ng-model 指令将 HTML 控件(input, select, textarea)的值绑定到应用程序数据。

ng-bind 指令将应用程序数据绑定到 HTML 视图。

AngularJS-Beispiel

<!DOCTYPE html>
<html>
<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
  <p>Name: <input type="text" ng-model="name"></p>
  <p ng-bind="name"></p>
</div>
</body>
</html>

Versuchen Sie es selbst aus

示例解释:

当网页加载完成后,AngularJS 会自动启动。

ng-app 指令告诉 AngularJS,<div> 元素是 AngularJS 应用程序的“所有者”。

ng-model 指令将输入字段的值绑定到应用程序变量 name.

ng-bind Instructions bind the content of the <p> element to the application variable name.

AngularJS-Instruktionen

As you can see, AngularJS instructions are with ng prefix HTML attributes.

ng-init Instructions initialize AngularJS application variables.

AngularJS-Beispiel

<div ng-app="" ng-init="firstName='Bill'">
<p>Name is <span ng-bind="firstName"></span></p>
</div>

Versuchen Sie es selbst aus

or use valid HTML:

AngularJS-Beispiel

<div data-ng-app="" data-ng-init="firstName='Bill'">
<p>Name is <span data-ng-bind="firstName"></span></p>
</div>

Versuchen Sie es selbst aus

If you want the HTML page to be valid, you can use data-ng- instead of ng-.

You will learn more about instructions later in this tutorial.

AngularJS- Ausdrücke

AngularJS expressions are written within double curly braces:{{ expression }}.

AngularJS will "output" data at the location of the expression:

AngularJS-Beispiel

<!DOCTYPE html>
<html>
<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
  <p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>

Versuchen Sie es selbst aus

AngularJS expressions bind AngularJS data to HTML, with ng-bind The way of instructions is the same.

AngularJS-Beispiel

<!DOCTYPE html>
<html>
<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
  <p>Name: <input type="text" ng-model="name"></p>
  <p>{{name}}</p>
</div>
</body>
</html>

Versuchen Sie es selbst aus

You will learn more about expressions later in this tutorial.

AngularJS-Anwendung

AngularJS ModuleDefine the AngularJS application.

AngularJS ControllerControl the AngularJS application.

ng-app Definition of the application by instructions,ng-controller Definition of controller instructions.

AngularJS-Beispiel

<div ng-app="myApp" ng-controller="myCtrl">
Name: <input type="text" ng-model="firstName"><br>
Nachname:<input type="text" ng-model="lastName"><br>
<br>
Vollständiger Name:{{firstName + " " + lastName}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.firstName= "Bill";
  $scope.lastName= "Gates";
});
</script>

Versuchen Sie es selbst aus

AngularJS-Module definieren die Anwendung:

AngularJS-Module

var app = angular.module('myApp', []);

AngularJS-Kontroller steuern die Anwendung:

AngularJS-Kontroller

app.controller('myCtrl', function($scope) {
  $scope.firstName= "Bill";
  $scope.lastName= "Gates";
});

Sie werden im folgenden Tutorial mehr über Module und Kontroller erfahren.