Angularjs基础知识及示例汇总

网络编程 2021-07-04 21:47www.168986.cn编程入门
本文给大家总结了一些angularjs的基础知识及相关示例,分享给大家,希望能对大家有所帮助。

angularjs是google开发的一款高大上的前端mvc开发框架。

Angularjs官网: 官网有demo,访问可能需要FQ

Angularjs中国社区: 适合初学者

引用文件:

使用angular注意

引用angularjs库:.... 可以在本节示例的github上下载
需要在你使用的区域加上ng-app="appName",或者直接ng-app(全局)。
设置控制器 ng-controller="Ctrl"。
测试一下示例请注意以下几点

需要在head之前引入angularjs代码,作者使用的是angular-1.0.1.min.js,请注意版本区别。
所有小示例都是在以下区域运行,记得在作用区域加上 ng-app。
狼蚁网站SEO优化通过一些小的案例来说明angularjs默认的常见的指令和用法。

hello world程序(双数据绑定)

使用ng-model={{name}}来绑定数据

代码如下:

<label for="name">name:</label>
<input type="text" ng-model="name" id="name"/>
<hr>
hello:{{name || 'liteng'}}

 

事件绑定使用小案例

代码如下:

<div>
  单价:<input type="number" min=0 ng-model="price" ng-init="price=299">
  数量: <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:设定初始值</dd>
  </dt>
</div>

 

ng-init:可默认指定属性值

代码如下:

<p ng-init="value='hello world'">{{value}}</p>

 

ng-repeat:用于迭代数据类似于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>

 

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>

 

ng-show:设置元素显示

注:ng-show="!xx":在属性值前面加!表示确定显示,如果不加!表示不确定则不显示

代码如下:

<div ng-show="!show">
  ng-show="!show"
</div>
<div ng-show="show">
  ng-show="show"
</div>

 

ng-hide:设置元素隐藏

代码如下:

<div ng-hide="aaa">
  ng-hide="aaa"
</div>
<div ng-hide="!aaa">
  ng-show="!aaa"
</div>

 

运用ng-show制作toggle效果

代码如下:

<h2>toggle</h2>
  <a href ng-click="showLog=!showLog">显示logo</a>
  <div ng-show="showLog">
    <img ng-src="" alt="">
  </div>

 

ng-style:和默认style类似

这里请注意书写格式:字符串需要用引号包含

代码如下:

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

 

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-->

 

ng-template:可以加载模板

代码如下:

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

 tpl.html

代码如下:

<h1>hello</h1>

 

$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',
           
         });
         p.suess(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').suess(function(response){
             $scope.info=response;
        });
      }
</script>

 

以上所有的code:https://github./litengdesign/angularjsTest

实现的demo:http://2.liteng.sinaapp./angularjsTest/index.html

至于angularjs的路由(router)和指令(directive)下次本人将单独拿出来讲。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by