国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Angularjs基礎(chǔ)知識(shí)及示例匯總

2024-05-06 16:15:05
字體:
供稿:網(wǎng)友
本文給大家總結(jié)了一些angularjs的基礎(chǔ)知識(shí)及相關(guān)示例,分享給大家,希望能對(duì)大家有所幫助。
 

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"。
測(cè)試一下示例請(qǐng)注意以下幾點(diǎn)

需要在head之前引入angularjs代碼,作者使用的是angular-1.0.1.min.js,請(qǐng)注意版本區(qū)別。
所有小示例都是在以下區(qū)域運(yùn)行,記得在作用區(qū)域加上 ng-app。
下面通過一些小的案例來說明angularjs默認(rèn)的常見的指令和用法。

hello world程序(雙數(shù)據(jù)綁定)

使用ng-model={{name}}來綁定數(shù)據(jù)

 

復(fù)制代碼代碼如下:

<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

事件綁定使用小案例

 

復(fù)制代碼代碼如下:

<div>
  單價(jià):<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>
  總價(jià):{{(price) * (quantity)}}
  <dt>
    <dl>注:</dl>
    <dd>涉及html5的input:<a href="http://www.w3school.com.cn/html5/att_input_type.asp">http://www.w3school.com.cn/html5/att_input_type.asp</a></dd>
    <dd>ng-init:設(shè)定初始值</dd>
  </dt>
</div>

 

 http://2.liteng.sinaapp.com/angularjsTest/event-bind.html

ng-init:可默認(rèn)指定屬性值

 

復(fù)制代碼代碼如下:

<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

 

復(fù)制代碼代碼如下:

<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的點(diǎn)擊事件

 

復(fù)制代碼代碼如下:

<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":在屬性值前面加!表示確定顯示,如果不加!表示不確定則不顯示

 

復(fù)制代碼代碼如下:

<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è)置元素隱藏

 

復(fù)制代碼代碼如下:

<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

運(yùn)用ng-show制作toggle效果

 

復(fù)制代碼代碼如下:

<h2>toggle</h2>
  <a href ng-click="showLog=!showLog">顯示logo</a>
  <div ng-show="showLog">
    <img ng-src="http://liteng.org/sites/default/files/logo.png" style="border-left-color: rgb(0, 153, 204); border-left-width: 1px; border-left-style: solid; padding: 0px 3px; margin: 3px auto 0px; width: 640px; background-color: rgb(242, 246, 251); clear: both; border-top-color: rgb(0, 153, 204); border-top-width: 1px; border-top-style: solid; border-right-color: rgb(0, 153, 204); border-right-width: 1px; border-right-style: solid;"> 復(fù)制代碼代碼如下:

<div ng-style="{width:100+'px',height:200+'px',backgroundColor:'red'}">
  box
</div>

 http://2.liteng.sinaapp.com/angularjsTest/ng-style.html

 

filter:過濾字段

 

復(fù)制代碼代碼如下:

<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:可以加載模板

 

復(fù)制代碼代碼如下:

<div ng-include="'tpl.html'"></div>

 

 tpl.html

 

復(fù)制代碼代碼如下:

<h1>hello</h1>

 http://2.liteng.sinaapp.com/angularjsTest/show-tpl.html

 

$http:一個(gè)類似ajax的方法很管用

 

復(fù)制代碼代碼如下:

<div class="container" ng-controller="TestCtrl">
  <h2>HTTP請(qǐng)求-方法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

實(shí)現(xiàn)的demo:http://2.liteng.sinaapp.com/angularjsTest/index.html

至于angularjs的路由(router)和指令(directive)下次本人將單獨(dú)拿出來講。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 濉溪县| 富阳市| 都昌县| 旺苍县| 集贤县| 饶平县| 中方县| 樟树市| 朔州市| 定远县| 嵩明县| 唐山市| 葵青区| 江西省| 五指山市| 成安县| 珲春市| 琼结县| 于田县| 白朗县| 自治县| 潮安县| 安平县| 图们市| 广平县| 瑞金市| 富顺县| 永靖县| 沛县| 金川县| 黄骅市| 衡阳市| 西昌市| 南雄市| 乌鲁木齐市| 通州区| 临洮县| 万载县| 天柱县| 浑源县| 濮阳县|