溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

基于AngularJS的簡單使用詳解

發(fā)布時間:2020-08-19 17:54:50 來源:腳本之家 閱讀:169 作者:mywifelidan 欄目:web開發(fā)

Angular Js 的初步認識和使用

一:

1.模塊化

定義模塊和控制器 ng-app="myapp" controller="myctrl"

指定模型 ng-model=""

獲取的屬性值: ng-bind="屬性名"或者{{屬性名}}

2.初始化模塊(在Script中進行)

var myapp1 =angular.module("myapp",[]);

3.定義模塊的控制器,并依賴注入,
$scope:可以操作模塊作用域內(nèi)的所有視圖

myapp1.controller("myctrl",["$scope",function($scope){
$scope."屬性名"
// 也可以對$scope操作的視圖進行賦值
$scope."屬性名"="值";
}])

4.綁定事件

//其他事件聯(lián)想基本為:ng-動作
ng-click="clear()" 

在定義的模塊控制器中進行事件函數(shù)的后續(xù)操作:
$scope.clearSop=function(){
$scope.name="";
}

5.集合數(shù)據(jù)的遍歷

數(shù)據(jù)格式范例:對象數(shù)組一般的數(shù)據(jù)類型(進行頁面數(shù)據(jù)交互時因注意json的數(shù)據(jù)格式)

$scope.products = [
{
id : 1001,
name : '數(shù)碼相機',
price : 3000
},{
id : 1002,
name : '蘋果手機',
price : 7000
}
];

遍歷樣式:product相當于元素,products相當于集合,index為索引

<tr ng-repeat="product in products">
<td>{{$index+1}}</td>
<td>{{product.id}}</td>
<td>{{product.name}}</td>
<td>{{product.price}}</td>
</tr>

6.AngularJS中的路由的使用

1.需要單獨導入:angular-route.js文件

2.定義angular模塊

3.初始化模塊

4.路由中的路徑格式采用:"#/+url"

5.使用ng-view的模塊用來展示路由加載后的變化內(nèi)容

6.初始化模塊:

var myapp1=angular.module("myapp",["noRoute"]);

7.配置模塊的路由

myapp.config(["$routeProvider", function($routeProvider){
$routeProvider
 .when('/JavaEE', {
 templateUrl: '5_1.html'
 })
 .when('/IOS', {
 templateUrl: '5_2.html'
 })
 .when('/Android', {
 templateUrl: '5_3.html'
 })
 .otherwise({
 redirectTo: '/JavaEE'
 });
}]);

以上這篇基于AngularJS的簡單使用詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI