標籤:
angular.js路由功能
用於實現單頁應用
//html 代碼<div ng-view></div>//js代碼angular.module(‘myM1‘,[‘ng‘,‘ngRoute‘]).config([‘$routeProvider‘,function($routeProvider){$routeProvider.when(‘/‘,{template:‘這是首頁頁面‘}).when(‘/computers‘,{template:‘這是電腦分類頁面‘}).when(‘/printers‘,{templateUrl:‘printers.html‘}).otherwise({redirectTo:‘/‘});}])
angular.js自訂指令
app.directive(‘runoobDirective‘,function(){ return { template:‘<span>這個是自訂指令!</span>‘ }; })
angular.js包含外部html檔案
<div ng-include="‘myUsers_List.htm‘"></div>
angular.js ng-app指令意義
<html ng-app>
angularjs啟動產生視圖時,會根據ng-app元素同$rootScope進行綁定。$rootScope是所有$scope對象的最上層。
$rootScope相當於JavaScript的全域,使用頻繁使用$rootscope是不太好的,相當於汙染了全域。
指令通常不會自己建立$scope,但也有例外,例如ng-controller,ng-repeat 指令會建立自己的子範圍,並將它們附加到DOM元素上。
angular.js自訂指令,模組
///自訂指令,或組件<my-directive> </my-directive> <div my-directive></div> <div class="my-directive"></div>app.directive(‘myDirective‘,function(){ return { restrict:‘EAC‘,//element , attr(推薦) , class ,m->注釋 replace:true,//全部都用template替換 template:‘<a href="http://www.baidu.com">click me </a>‘ }; })
angular.js動態產生並雙向資料繫結
//模板產生並雙向繫結<label for="">Their URL field: </label> <input type="text" ng-model="theirUrl"/><div my-directive some-attr = ‘theirUrl‘ my-link-text = ‘click me to go to google‘ ></div>.directive(‘myDirective‘,function(){ return { restrict:‘EAC‘,//element , attr(推薦) , class ,m->注釋 replace:true,//全部都用template替換 scope:{ myUrl: ‘=someAttr‘, myLinkText: ‘@‘ }, template:‘<div><label>My Url Field:</label> <input type="text" ng-model="myUrl"/>\ <a href="{{myUrl}}">{{myLinkText}}</a></div>‘ }; })
全域rootscope如何訪問
//在angulard代碼中可以使用run方法來訪問$rootscope
app.run(function($rootScope){$rootScope.rootScope = ‘hello word‘})
//ng-if 和ng-show ,ng-hide 的區別是ng-if會真正的移除或產生節點。ng-show 是通過css來顯示或隱藏節點。
//ng-repeat 遍曆集合
會暴露一些特殊的屬性
$index 遍曆到第幾個值 //值是從0 開始到length-1 ,所以奇數02468,偶數135
$first 遍曆到第一個值的時候為true
$middle 遍曆處於第一個與最後一個之間的值是為true
$last 遍曆到最後一個值的時候為true
$even $index為偶數的時候為true
$odd $index為奇數的時候為true
//ng-init 指令用來設定內部範圍的初始狀態.
//ng-cloak 也可以避免閃爍
<p ng-cloak>{{name}}</p>
//ng-bind-template,用於綁定多個運算式
<p ng-bind-template="{{message}}{{name}}">
//ng-change//輸入的值發生改變時觸發的事件
<input type="text" ng-model="equation" ng-change="change()"/>
$scope.change= function(){
console.log($scope.equation)
}
//directive 自訂指令的全部屬性有
restrict: 指定angular指令在dom中以哪種方式聲明,預設為a(attr),e(ele),c(class),m(comments),
可以同時指定多個 restrict:‘EAMC‘
priority: 優先順序,數字類型 預設為零 (優先順序最高的指令是ng-repeat)
terminal: 布爾值.比本指令優先順序低的指令會停止。同一等級的指令不會被影響
template: 字串或函數,可選,必須有一個(htm文本)或(可以接受兩個參數的函數(temement,tattrs))
注意1,如果有多個同等級的標籤,必須要有一根標籤包含全部的標籤
注意2,如果要換行,注意在每一行的後面加上反斜線(推薦使用templateUrl來引入外部的模板)
templateUrl: 選擇性參數1.html檔案路徑的字串.2.(可以接受兩個參數的函數(temement,tattrs))
replace: 布爾值 預設為false.模板預設插入到自訂指令的內部:為true則是直接插入
還有一些屬性就等下篇。
angular學習筆記01