最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

AngularJS 最常用的八種功能(基礎(chǔ)知識)

 更新時間:2017年06月26日 15:45:44   作者:SunsCheung  
這篇文章主要介紹了AngularJS 最常用的八種功能,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧

AngularJS 使用基礎(chǔ)知識

第一 迭代輸出之ng-repeat標(biāo)簽

ng-repeat讓table ul ol等標(biāo)簽和js里的數(shù)組完美結(jié)合

<ul>
<li ng-repeat="person in persons">
{{person.name}} is {{person.age}} years old.
</li>
</ul>

你甚至可以指定輸出的順序:

<li ng-repeat="person in persons | orderBy:'name'">

第二 動態(tài)綁定之ng-model標(biāo)簽 任何有用戶輸入,只要是有值的html標(biāo)簽,都可以動態(tài)綁定js中的變量, 而且是動態(tài)綁定。

<input type="text" ng-model='password'>

對于綁定的變量,你可以使用{{}} 直接引用

<span>you input password is {{password}}</span>

如果你熟悉fiter,你可以很容易的按你的需要格式輸出

<span>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span>

第三 綁定點擊事件之ng-click事件

使用ng-click你可以很容易的為一個標(biāo)簽綁定點擊事件。

<button ng-click="pressMe()"/>

當(dāng)然前提是你要在$scope域中定義的自己的pressMe方法。

和傳統(tǒng)的onclick方法不同,你甚至可以為ng-click方法傳遞一個對象,就像這樣:

<ul>
<li ng-repeat="person in persons">
<button ng-click="printf(person)"/>
</li>
</ul>

當(dāng)然還有ng-dblclick標(biāo)簽

第四 分支語句之ng-switch on、ng-if/ng-show/ng-hide/ng-disabled標(biāo)簽

分支語句讓你在界面上都可以寫邏輯

<ul>
<li ng-repeat="person in persons">
<span ng-switch on="person.sex">
<span ng-switch-when="1">you are a boy</span>
<span ng-switch-when="2">you are a girl</span>
</span>
<span ng-if="person.sex==1">you may be a father</span>
<span ng-show="person.sex==2">you may be a mother</span>
<span>
please input your baby's name:<input type="text" ng-disabled="!person.hasBaby"/>
</span>
<span>
</li>
</ul>

校驗語法之ng-trim ng-minlength ng-maxlength required ng-pattern 等標(biāo)簽

表單中的輸入框,你可以使用上面的標(biāo)簽來實現(xiàn)對用戶輸入的校驗。

從字面意思上你已經(jīng)知道了它們的意思。

<form name="yourForm">
<input type="text" name="inputText" required ng-trim="true" ng-model="userNum" ng-pattern="/^[0-9]*[1-9][0-9]*$/" ng-maxlength="6" maxlength="6"/>
</form>

你可以通過 scope.yourForm.inputText.error.required 來判斷輸入框是否為空

你可以通過 scope.yourForm.inputText.invalid 來判斷輸入的內(nèi)容是否滿足ng-pattern,ng-maxlength,maxlength

你通過$scope.userNum獲得的輸入內(nèi)容是去掉前后空白的,因為ng-trim的存在。

第六 下拉框之ng-options標(biāo)簽

ng-options是為下拉框?qū)iT打造的標(biāo)簽。

<select ng-model="yourSelected" ng-options="person.id as person.name in persons"></select>

下拉框中顯示的是person.name,當(dāng)你選中其中一個的時候,你可以通過yourSelected得到你選中的person.id.

第七 控制css之ng-style標(biāo)簽

ng-style幫你輕松控制你的csyour color通過給myColor賦值的形式來改變你想要的效果,就像scope.myColor=color:′blue′;scope.myColor={cursor: 'pointer',color:'blue'}; 異步請求之http對象。AngularJS提供了一個類似jquery的.ajax的對象,用于異步請求。

在AngularJS中對異步操作是推崇至極的,所以$http的操作都是異步的不像jquery.ajax里還提供了async參數(shù)。

$http({method : 'POST',params : { id:123}, data:{name:'john',age:27}, url : "/mypath"})
.success(function(response, status, headers, config){
//do anything what you want;
})
.error(function(response, status, headers, config){
//do anything what you want;                        
});

如果你是POST請求,params里的數(shù)據(jù)會幫你拼到url后面,data里的數(shù)據(jù)會放到請求體中。

以上所述是小編給大家介紹的AngularJS 最常用的八種功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論

赤城县| 吉隆县| 石门县| 贵阳市| 吐鲁番市| 曲松县| 临夏市| 兴义市| 齐齐哈尔市| 金秀| 庄浪县| 梧州市| 靖远县| 泸水县| 乌兰察布市| 根河市| 定南县| 定襄县| 睢宁县| 蒙城县| 莱芜市| 常德市| 遂溪县| 贵南县| 申扎县| 扶风县| 大宁县| 育儿| 客服| 科尔| 灵丘县| 高尔夫| 泾川县| 金阳县| 都昌县| 阜城县| 佛学| 开远市| 平昌县| 电白县| 伊宁县|