国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

AngularJS控制器詳解及示例代碼

2019-11-20 09:13:38
字體:
來源:轉載
供稿:網友

AngularJS應用主要依賴于控制器來控制數據在應用程序中的流動。控制器采用ng-controller指令定義。控制器是一個包含屬性/屬性和JavaScript對象的功能。每個控制器接受$scope參數指定應用程序/模塊,由控制器控制。

<div ng-app="" ng-controller="studentController">...</div>

在這里,我們已經聲明采用ng-controller指令的控制器studentController。作為下一步,我們將定義studentController如下

<script>function studentController($scope) {  $scope.student = {   firstName: "yiibai",   lastName: "com",   fullName: function() {     var studentObject;     studentObject = $scope.student;     return studentObject.firstName + " " + studentObject.lastName;   }  };}</script>

studentController 定義 $scope 作為JavaScript對象參數。

$scope 表示應用程序,使用studentController對象。

$scope.student 是studentController對象的屬性。

firstName和lastName是$scope.student 對象的兩個屬性。我們已經通過了默認值給他們。

fullName 是$scope.student對象的函數,它的任務是返回合并的名稱。

在fullName函數中,我們現在要學生對象返回組合的名字。

作為一個說明,還可以定義控制器對象在單獨的JS文件,并把有關文件中的HTML頁面。

現在可以使用ng-model或使用表達式如下使用studentController學生的屬性。

Enter first name: <input type="text" ng-model="student.firstName"><br>Enter last name: <input type="text" ng-model="student.lastName"><br><br>You are entering: {{student.fullName()}}

現在有 student.firstName 和 student.lastname 兩個輸入框。

現在有 student.fullName()方法添加到HTML。

現在,只要輸入first name和lastname輸入框中輸入什么,可以看到兩個名稱自動更新。

例子

下面的例子將展示使用控制器。

testAngularJS.html 文件內容如下:

<html><head><title>Angular JS Controller</title></head><body><h2>AngularJS Sample Application</h2><div ng-app="" ng-controller="studentController">Enter first name: <input type="text" ng-model="student.firstName"><br><br>Enter last name: <input type="text" ng-model="student.lastName"><br><br>You are entering: {{student.fullName()}}</div><script>function studentController($scope) {  $scope.student = {   firstName: "Mahesh",   lastName: "Parashar",   fullName: function() {     var studentObject;     studentObject = $scope.student;     return studentObject.firstName + " " + studentObject.lastName;   }  };}</script><script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script></body></html>

輸出

在Web瀏覽器打開textAngularJS.html,看到以下結果:

以上就是AngularJS控制器的資料整理,后續繼續整理相關知識,謝謝大家對本站的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长兴县| 封丘县| 富锦市| 黎平县| 岫岩| 海城市| 友谊县| 南漳县| 武义县| 山东省| 全椒县| 新和县| 怀来县| 泾源县| 漳浦县| 大庆市| 湖北省| 华蓥市| 盐池县| 天气| 阳高县| 革吉县| 志丹县| 渝中区| 吉安县| 铁力市| 凯里市| 大港区| 阿坝县| 富锦市| 广元市| 军事| 龙游县| 天长市| 安丘市| 登封市| 益阳市| 沙洋县| 革吉县| 泽库县| 云龙县|