Home  >  Article  >  Web Front-end  >  How to use AngularJs controller? Usage examples of angularjs controller

How to use AngularJs controller? Usage examples of angularjs controller

寻∝梦
寻∝梦Original
2018-09-08 13:49:301042browse

This article is an introduction to the angularjs controller, an analysis of usage examples of the angularjs controller, and there are also application methods for the angularjs controller. Let’s take a look at this article together
AngularJS Controller

## AngularJS Controller Control data for AngularJS applications. AngularJS controllers are regular JavaScript objects.
AngularJS applications are controlled by controllers. The
ng-controller directive defines an application controller.
Controllers are JavaScript objects created by the standard JavaScript object constructor.

AngularJS Example

<p ng-app="myApp" ng-controller="myCtrl">

名: <input type="text" ng-model="firstName"><br>
姓: <input type="text" ng-model="lastName"><br>
<br>
姓名: {{firstName + " " + lastName}}

</p>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
</script>

尝试一下 »

AngularJS 应用程序由 ng-app 定义。应用程序在 <p> 内运行。
ng-controller="myCtrl" 属性是一个 AngularJS 指令。用于定义一个控制器。
myCtrl 函数是一个 JavaScript 函数。
AngularJS 使用$scope 对象来调用控制器。
在 AngularJS 中, $scope 是一个应用象(属于应用变量和函数)。
控制器的 $scope (相当于作用域、控制范围)用来保存AngularJS Model(模型)的对象。
控制器在作用域中创建了两个属性 (firstName 和 lastName)。
ng-model 指令绑定输入域到控制器的属性(firstName 和 lastName)。
控制器方法
上面的实例演示了一个带有 lastName 和 firstName 这两个属性的控制器对象。
控制器也可以有方法(变量和函数):
AngularJS 实例
<p ng-app="myApp" ng-controller="personCtrl">

名: <input type="text" ng-model="firstName"><br>
姓: <input type="text" ng-model="lastName"><br>
<br>
姓名: {{fullName()}}
</p>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
    $scope.fullName = function() {
        return $scope.firstName + " " + $scope.lastName;
    }
});
</script>
尝试一下 »
外部文件中的控制器
在大型的应用程序中,通常是把控制器存储在外部文件中。
只需要把 <script> 标签中的代码复制到名为 personController.js 的外部文件中即可:
AngularJS 实例
<p ng-app="myApp" ng-controller="personCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</p>
<script src="personController.js"></script>
尝试一下 »
其他实例
以下实例创建一个新的控制器文件:
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [
        {name:'Jani',country:'Norway'},
        {name:'Hege',country:'Sweden'},
        {name:'Kai',country:'Denmark'}
    ];
});
保存文件为  namesController.js:
然后,在应用中使用控制器文件:
<p ng-app="myApp" ng-controller="namesCtrl">
<ul>
  <li ng-repeat="x in names">
    {{ x.name + ', ' + x.country }}
  </li>
</ul>
</p>
<script src="namesController.js"></script>
Okay, this article is over (if you want to see more, go to the PHP Chinese website

AngularJS use (Study in manual ), if you have any questions, you can leave a message below.

The above is the detailed content of How to use AngularJs controller? Usage examples of angularjs controller. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn