Angularjs基礎(chǔ)知識及示例匯總
angularjs是google開發(fā)的一款高大上的前端mvc開發(fā)框架。
Angularjs官網(wǎng):https://angularjs.org/ 官網(wǎng)有demo,訪問可能需要FQ
Angularjs中國社區(qū):http://www.angularjs.cn/ 適合初學(xué)者
引用文件:https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js
使用angular注意
引用angularjs庫:https://github.com/litengdesign/angularjsTest/blob/master/angular-1.0.1.... 可以在本節(jié)示例的github上下載
需要在你使用的區(qū)域加上ng-app="appName",或者直接ng-app(全局)。
設(shè)置控制器 ng-controller="Ctrl"。
測試一下示例請注意以下幾點
需要在head之前引入angularjs代碼,作者使用的是angular-1.0.1.min.js,請注意版本區(qū)別。
所有小示例都是在以下區(qū)域運行,記得在作用區(qū)域加上 ng-app。
下面通過一些小的案例來說明angularjs默認(rèn)的常見的指令和用法。
hello world程序(雙數(shù)據(jù)綁定)
使用ng-model={{name}}來綁定數(shù)據(jù)
<label for="name">name:</label>
<input type="text" ng-model="name" id="name"/>
<hr>
hello:{{name || 'liteng'}}
http://2.liteng.sinaapp.com/angularjsTest/helloangularjs.html
事件綁定使用小案例
<div>
單價:<input type="number" min=0 ng-model="price" ng-init="price=299">
數(shù)量: <input type="number" min=0 ng-model="quantity" ng-init="quantity=1">
<br>
總價:{{(price) * (quantity)}}
<dt>
<dl>注:</dl>
<dd>涉及html5的input:<a href=">
<dd>ng-init:設(shè)定初始值</dd>
</dt>
</div>
http://2.liteng.sinaapp.com/angularjsTest/event-bind.html
ng-init:可默認(rèn)指定屬性值
<p ng-init="value='hello world'">{{value}}</p>
http://2.liteng.sinaapp.com/angularjsTest/ng-init.html
ng-repeat:用于迭代數(shù)據(jù)類似于js中的 i for info
<div ng-init="friends=[{name:'Jhon',age:25},{name:'Mary',age:28}]"></div>
<p>我有{{friends.length}} 朋友.他們是</p>
<ul>
<li ng-repeat="friend in friends">
[{{$index+1}}]:{{friend.name}}年齡為:{{friend.age}}
</li>
</ul>
http://2.liteng.sinaapp.com/angularjsTest/ng-repeat.html
ng-click:dom的點擊事件
<div ng-controller="ctrl">
<button ng-dblclick='showMsg()'>{{a}}</button>
</div>
<script>
function ctrl($scope){
$scope.a='hello';
$scope.showMsg=function(){
$scope.a='world';
}
}
</script>
http://2.liteng.sinaapp.com/angularjsTest/ng-click.html
ng-show:設(shè)置元素顯示
注:ng-show="!xx":在屬性值前面加!表示確定顯示,如果不加!表示不確定則不顯示
<div ng-show="!show">
ng-show="!show"
</div>
<div ng-show="show">
ng-show="show"
</div>
http://2.liteng.sinaapp.com/angularjsTest/ng-show.html
ng-hide:設(shè)置元素隱藏
<div ng-hide="aaa">
ng-hide="aaa"
</div>
<div ng-hide="!aaa">
ng-show="!aaa"
</div>
http://2.liteng.sinaapp.com/angularjsTest/ng-hide.html
運用ng-show制作toggle效果
<h2>toggle</h2>
<a href ng-click="showLog=!showLog">顯示logo</a>
<div ng-show="showLog">
<img ng-src=" </div>
http://2.liteng.sinaapp.com/angularjsTest/ng-toggle.html
ng-style:和默認(rèn)style類似
這里請注意書寫格式:字符串需要用引號包含
<div ng-style="{width:100+'px',height:200+'px',backgroundColor:'red'}">
box
</div>
http://2.liteng.sinaapp.com/angularjsTest/ng-style.html
filter:過濾字段
<div>{{9999|number}}</div> <!--9,999-->
<div>{{9999+1 |number:2}}</div><!--10,000.00-->
<div>{{9*9|currency}}</div><!--$81.00-->
<div>{{'hello world' | uppercase}}</div><!--HELLO WORLD-->
http://2.liteng.sinaapp.com/angularjsTest/filter.html
ng-template:可以加載模板
<div ng-include="'tpl.html'"></div>
tpl.html
<h1>hello</h1>
http://2.liteng.sinaapp.com/angularjsTest/show-tpl.html
$http:一個類似ajax的方法很管用
<div class="container" ng-controller="TestCtrl">
<h2>HTTP請求-方法1</h2>
<ul>
<li ng-repeat="x in names">
{{x.Name}}+{{x.Country}}
</li>
</ul>
</div>
<h2>方法2</h2>
<div ng-controller="TestCtrl2">
<ul>
<li ng-repeat="y in info">
{{y.aid}}+{{y.title}}
</li>
</ul>
</div>
<script>
//方法1
var TestCtrl=function($scope,$http){
var p=$http({
method:'GET',
url:'json/date.json'
});
p.success(function(response,status,headers,config){
$scope.names=response;
});
p.error(function(status){
console.log(status);
});
}
//方法2
function TestCtrl2($scope,$http){
$http.get('json/yiqi_article.json').success(function(response){
$scope.info=response;
});
}
</script>
http://2.liteng.sinaapp.com/angularjsTest/ajax.html
以上所有的code:https://github.com/litengdesign/angularjsTest
實現(xiàn)的demo:http://2.liteng.sinaapp.com/angularjsTest/index.html
至于angularjs的路由(router)和指令(directive)下次本人將單獨拿出來講。
相關(guān)文章
Angularjs實現(xiàn)搜索關(guān)鍵字高亮顯示效果
本篇文章主要介紹了Angularjs實現(xiàn)搜索關(guān)鍵字高亮顯示的方法,具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01AngularJS基礎(chǔ) ng-class-odd 指令示例
本文主要介紹AngularJS ng-class-odd 指令,這里對ng-class-odd基礎(chǔ)知識做了詳細(xì)整理,并有示例代碼和效果圖,學(xué)習(xí)AngularJS的同學(xué)可以參考下2016-08-08angularJS+requireJS實現(xiàn)controller及directive的按需加載示例
本篇文章主要介紹了angularJS+requireJS實現(xiàn)controller及directive的按需加載示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02Angular angular-file-upload文件上傳的示例代碼
這篇文章主要介紹了Angular angular-file-upload文件上傳的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08ionic4+angular7+cordova上傳圖片功能的實例代碼
ionic是一個垮平臺開發(fā)框架,可通過web技術(shù)開發(fā)出多平臺的應(yīng)用。這篇文章主要介紹了ionic4+angular7+cordova上傳圖片功能,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-06-06angularjs使用directive實現(xiàn)分頁組件的示例
本篇文章主要介紹了angularjs使用directive實現(xiàn)分頁組件的示例,具有一定的參考價值,有興趣的可以了解一下。2017-02-02AngularJS中$apply方法和$watch方法用法總結(jié)
這篇文章主要介紹了AngularJS中$apply方法和$watch方法用法,結(jié)合實例形式總結(jié)分析了$apply方法和$watch方法的功能、參數(shù)含義、使用技巧與相關(guān)注意事項,需要的朋友可以參考下2016-12-12