標籤:嵌入 json prim link 選擇 ict direct tar port
用A連結化物件解析url的組成
var url = ‘https://www.baidu.com:8080/aaa/1.html?id=10#name‘;
var aLink = document.createElement(‘a‘);
aLink.href = url;
console.log(aLink);//<a href="https://www.baidu.com:8080/aaa/1.html?id=10#name"></a>
console.log(aLink.search);//?id=10
console.log(aLink.hash);//#name
...
// protocol: https
// host: www.baidu.com:8080
// hostname: www.baidu.com
// port: 8080
// pathname: /aaa/1.html
// search: ?id=10
// hash: #name
JSON就是用字串描述對象的方式
angular.js常用指令:
AngularJS 屬性以 ng- 開頭,但是您可以使用 data-ng- 來讓網頁對 HTML5 有效。
ng-app:ng-app指令用來標明一個AngularJS應用程式,標記在一個AngularJS的作用範圍的根對象上,系統執行時會自動的執行根物件範圍內的其他指令,可以在同一個頁面建立多個ng-app節點,但不推薦。建立多個ng-app時,預設只能執行第一個,後面的需要手動引導:angular.bootstrap(document.querySelector(‘[ng-app="myApp2"]‘),[‘myApp2‘]);但一般不用這種方式引導;正確方式為:angular.module(‘myApp‘, [‘myApp1‘, ‘myApp2‘]);
ng-bind:用法樣本:
<!-- <strong>{{username}}</strong> -->
<strong ng-bind="username"></strong>
<!-- ng-bind指令在綁定的值包含HTML時會轉義,為了安全(跨站指令碼攻擊) -->
如果確實需要將HTML正確輸入,使用的是ng-bind-html,同時需要引進angular-sanitize.js包,並建立一個依賴於此包的模組:angular.module(‘myApp‘,[‘ngSanitize‘]);最後確保一定要使用自己定義的模組才有效果。
ng-repeat:ng-repeat指令用來編譯一個數組重複建立當前元素,如 <li ng-repeat="item in app">
<strong>{{item.name}}</strong>
<span>{{item.age}}</span>
</li>
當app中有重複值時會報錯,此時應該這樣寫:name in app track by $index;
(補充:name.startsWith(lastname):以lastname為開頭的name的值為true----這是H5新增元素)
ng-class:ng-class指令可以設定一個索引值對,用於決定是否添加一個特定的類名,鍵為class名,值為bool類型表示是否添加該類名:ng-class="{red:$even,green:$odd}" ;ng-class也可以像普通class一樣使用
ng-show/ng-hide:會根據屬性值去確定是否展示當前元素,例如ng-show=false則不會展示該元素;
ng-if是指是否存在DOM元素;
ng-href/ng-src指令用於解決當連結類型的資料繫結時造成的載入BUG
ng-switch 指令根據運算式顯示或隱藏對應的部分。對應的子項目使用 ng-switch-when 指令,如果匹配選中選擇顯示,其他為匹配的則移除。你可以通過使用 ng-switch-default 指令設定預設選項,如果都沒有匹配的情況,預設選項會顯示。
ng-checked 指令用於設定複選框(checkbox)或選項按鈕(radio)的 checked 屬性。如果 ng-checked 屬性返回 true,複選框(checkbox)或選項按鈕(radio)將會被選中。
ng-selected 指令用於設定 <select> 列表中的 <option> 元素的 selected 屬性。ng-selected 屬性的運算式返回 true 則選項被選中。
常用事件指令與原生js基本相同,差別就是前面加上ng-;例如ng-click;
自訂指令(重要但不常用,個人覺得很難):
目的:封裝一些常用而且公用的東西,angular仍然還有一些DOM操作的可能,所有的dom操作都應該集中在自訂指令中。
以下demo預設引用bootstrap和angular
demo1:
script:
var demoApp = angular.module(‘demoApp‘, []);
// 第一個參數是指令的名字,第二個參數任然應該使用一個數組,數組的最後一個元素是一個函數
// 定義指令的名字,應該使用駝峰命名法
demoApp.directive(‘itcastButton‘, [function() {
// 該函數應該返回一個指令對象
return {
template:‘<input type="button" value="itcast" class="btn btn-lg btn-primary btn- block" />‘
};
}]);
html:
<itcast-button></itcast-button>//代替了template中的內容
demo2:
script:
demoApp.directive(‘btn‘, [function() {
return{
scope:{
primary:‘@‘,
lg:‘@‘,
block:‘@‘,
},
template:‘<button class="btn {{primary==\‘true\‘?\‘btn-primary\‘:\‘\‘}}">button</button>‘
}
}]);
html:
<btn primary="false"></btn>
demo3:
script:
demoApp.directive(‘btn‘, [function() {
return {
// transclude字面意思就是嵌入,也就是說你需不需要將你的指令內部的元素(注意不是指令的模板)嵌入到你的模板中去,預設是false
// 指令對象的transclude必須設定為true才可以在模版中使用ng-transclude指令
transclude: true,
replace: true, // 替換指令在HTML中綁定的元素
template: ‘<button class="btn btn-primary btn-lg" ng-transclude></button>‘
};
}]);
html:
<btn>itcast</btn>
demo4:(瞭解一些屬性意義即可)
script:
demoApp.directive(‘breadcrumb‘, [function() {
return {
// 指定當前指令的類型什麼樣的,預設情況下全部支援
restrict: ‘A‘,
//E = Element, A = Attribute, C = Class, M = Comment
// template: ‘‘, // 模版字串
templateUrl: ‘tmpls/breadcrumb.html‘,//引入模板
replace: true,
// transclude: true,
};
}]);
Angular.js學習筆記 (二)