Angular JS(1)

1、表达式

<!DOCTYPE html>
<html lang="en" ng-app="">
<head>
    <script src="./angular.min.js"></script>
</head>
<body ng-init="name='sun';age=13">
    {{ 5*4 }}
    {{ 5>4?"真":"假"}}
    <h1>{{name +"--"+age}}</h1>
    <input type="text" ng-model="one">
    *
    <input type="text" ng-model="two">
    =
    {{one*two}}
</body>

2、内置指令

凡是以ng- 开始的,都称为内置指令
ng-app 用于表示一个angularjs应用
Angular会从ng-app所在的标签开始,管理这个应用
一个页面(应用)中,建议只存在一个ng-app
如果有多个,需要手动启动对应的应用
目前ng-app=" "表示使用默认的angular 应用
ng-init='a=b;c=d;...';
使用 ng-init 初始的数据,会挂载到根作用域
在开发正式项目时,不建议使用ng-init做数据初始化
应该交由controller去完成
ng-model 将(表单)视图与模型进行(双向)绑定
ng-repeat 遍历对象
遍历数组:ng-repeat="x in arr"
遍历对象:ng-repeat=" (key,val) in obj"
属性: $first 是否是第一项
$last 是否是最后一项
$middle 是否是中间项
$index 下标值
track by $index Angular需要一个不重复的值去跟踪数据的变化,当数组内有重复时,将导致angular 无法正常跟踪对应的值,需要使用track by $index 为遍历指定一个唯一不重复的值。
ng-class为元素指定样式名
ng-class="{true:'class1',false:'class2'}[bol]" 由 Bol 决定添加哪个样式
ng-class="{'class1':bol1,'class2':bol2,'classN':bolN}"由各个变量决定是否添加指定的样式
ng-style 为元素添加样式
ng-style=" { style1:'style',...}"
ng-show是否显示元素,true显示,false隐藏
ng-show 为 false 时,只是为元素添加一个优先级最高的 display:none;
ng-if 是否显示元素
ng-if 为 false 时,会将元素从DOM 树种移除
当元素需要反复显示隐藏时,使用 ng-show
当元素只显示一次后就不再使用时,使用ng-if
ng-click 单击事件
ng-mouseover...dom的标准事件的写法
ng-bind 将模型输出到页面,后引入脚本时可以解决{{ }}造成的原样输出问题

  .box{width:100px;height:100px;background: red;}
        .box1{border:1px solid}
        .box2{border-radius: 5px;}
        .box3{box-shadow: 2px 2px 5px black}

<body ng-init="isBox1=true;isBox2=true;isBox3=true;styleObject={};isShow=true;html='<h2>htmlString<h2>'">
    <select name="" id="" ng-model="className">
        <option value="true">box1</option>
        <option value="false">box2</option>
    </select>
    <div class="box" ng-class="{true:'box1',false:'box2'}[className]"></div>
    <div class="box" ng-class="{'box1':isBox1,'box2':isBox2,'box3':isBox3}"></div>

    <div ng-style="{width:'100px',height:'100px',background:'green'}"></div>
    <div ng-style="styleObject"></div>

    <input type="checkbox" ng-model="isShow">显示/隐藏

    <button ng-click="isShow = ! isShow"> 显示、隐藏 </button>
    <div class="box" ng-show="isShow">1</div>
    <!--<div class="box" ng-show="!isShow">2</div>-->
    <div class="box" ng-if="isShow">2</div>

    <h1 >{{ isShow }}</h1>
    <h1 ng-bind="isShow"></h1>

    {{html}}
    <div ng-bind="html"></div>
    <div ng-bind-html="html"></div>
</body>

3、Controller

angular.module( moduleName[,depends(可选参数)]) 创建或获取模块
moduleName :模块名称
depends: 依赖的其它模块 Array
当只传入一个名称时,代表获取指定的模块
两个参数时,定义模块,即使不依赖其它模块,也要传入一个空的数组

<html lang="en" ng-app="myApp">

   var app = angular.module("myApp",[]);
    //推断式注入
    app.controller("myController",function($scope){
        $scope.name="sun";
    });
    //声明式注入(建议一律使用这种方式,代码压缩混淆后依然能正确运行)
    //数组的 0 到 倒数第二项 是该控制器需要注入的模块
    //最后一项是回掉函数
    app.controller("myController2",["$scope",function(s){
        s.name="tom";
    }])

$rootScope根作用域 在声明 ng-app 的位置创建此作用域
一个 angular 应用有且只有一个根作用域
同级的作用域不可相互访问

<body ng-controller="mainController">
    <div ng-controller="myController">
        {{name}}---{{age}}--{{main}}--{{address}}
    </div>
    <div ng-controller="myController2">{{name}}--{{age}}--{{main}}--{{address}}</div>
    <div ng-init="address='Beijing'">
        {{address}}
    </div>
    {{name}}--{{main}}--{{address}}
</body>

    var app = angular.module("myApp",[])
    app.controller("myController",["$scope",function($scope){
        $scope.name = "sun";
        $scope.age = 30
    }]);
    app.controller("myController2",["$scope",function($scope){
        $scope.name = "tom";
    }]);
    app.controller("mainController",["$scope",function($scope){
        $scope.main = "Hello Angular"
    }])

子级作用域可以访问使用父级作用域的值,但是无权修改

<body ng-controller="mainController">
    <h1>父级</h1>
    <input type="text" ng-model="pmsg">
    <div ng-controller="myController">
        <h1>子级</h1>
        <input type="text" ng-model="pmsg">
        这是父级的值:{{pmsg}}
        <h1 ng-repeat="item in items">{{item.name+'--'+item.price}}</h1>
        <button ng-click="clickEvent($event)">点击</button>
    </div>
</body>

    var app = angular.module("myapp",[]);
    app.controller("myController",["$scope",function($scope){
        $scope.cmsg = ""
        $scope.items = [{
            name:'iPhone',
            price : 3000
        },{
            name:'蓝莓',
            price : 4000
        }];
        $scope.clickEvent = function(e){
            console.log("点击触发",e);
            $scope.items.push({
                name:"新手机",
                price:9889
            })
        }
    }]);
    app.controller("mainController",["$scope",function($scope){
        $scope.pmsg = ""
    }])

内置过滤器

使用内置过滤器的方法是用 “管道符” “|” 链接
**currency ** 货币格式转换
currency :“前缀” 更改指定的前缀,默认为$
lowercass | uppercase 大小写转换
date 日期格式转换
y 年 M 月 d 日 H 24时 h 12时 m 分钟 s 秒
数组过滤器:
limitTo 限制结果条数 如:limitTo:2 显示两条
orderBy 排序
orderBy:orderKey 按orderKey升序排列
orderBy:orderKey:true 按orderKey 降序排列
filter 按关键字快速过滤
filter:searchKey 过滤所有数据包含 searchKey 的内容

 <h2 >{{ price }}</h2>
    <h2 >{{ price | currency}}</h2>
    <h2 >{{ price | currency:"¥"}}</h2>
    <h2 >{{ price | currency:"星星"}}</h2>
    <h2 >{{ str | lowercase}}</h2>
    <h2 >{{ str | uppercase}}</h2>
    <h2>{{date}}</h2>
    <h2>{{date | date:"yyyy-MM-dd HH:mm:ss a"}}</h2>
    <br>
    <!--<h3 ng-repeat="item in items | limitTo:2">{{item.name}}</h3>-->
    <!--<h3 ng-repeat="item in items | orderBy:'price':true">{{item.name}}--{{item.price}}</h3>-->

    <select name="" id="" ng-model="orderKey">
        <option value="name">按名称排序</option>
        <option value="price">按价格排序</option>
    </select>
    <input type="checkbox" ng-model="isDown">升/降
    <input type="text" ng-model="searchKey" 请输入关键字>
    <h3 ng-repeat="item in items | orderBy:orderKey:isDown | filter:searchKey">{{item.name +'---'+ item.price}}</h3>

  angular.module("myapp",[])
    .controller("myController",["$scope",function($scope){
        console.log($scope)
        $scope.price = 4999,
        $scope.str = "Hello Angular",
        $scope.date = new Date(),
        $scope.items = [{
            name:"iPhone9"
            ,price : 9000
        },{
            name : "iPhone5"
            ,price:3000
        },{
            name:"小米"
            ,price:5000
        },{
            name:"蓝莓"
            ,price:6000
        }]
    }])

自定义过滤器

 <select name="" id="" ng-model="orderKey">
        <option value="name">按名称排序</option>
        <option value="price">按价格排序</option>
    </select>
    <input type="checkbox" ng-model="isDown">升/降
    <input type="text" ng-model="searchKey" placeholder="输入关键字">
    <!--<h3 ng-repeat="item in items | orderBy:orderKey:isDown | filter:searchKey">{{item.name +'---'+ item.price}}</h3>-->
    {{"str" | myFilter}} {{[2,3,4,5] | myFilter2}}
    <h3 ng-repeat="item in items | myFilter3:searchKey">{{item.name}}--{{item.price}}</h3>

angular.module("myapp", [])
        .filter("myFilter", function () {
            return function (val) {
                console.log(val);
                // return val.toUpperCase();
                return val == "str" ? "good" : "no"
            }
        })
        .filter("myFilter2", function () {
            return function (val) {
                console.log(val);
                var arr = val.map(Math.sqrt)
                return arr;
            }
        })
        .filter("myFilter3", function () {
            return function (val,arg1) {
                console.log(val);
                console.log(arg1);
                var arr=[];
                val.forEach(function(item){
                    if(item.name.indexOf(arg1)!=-1){
                        arr.push(item)
                    }
                })
                return arr;
            }
        })

http://www.zhangxinxu.com/wordpress/2013/04/es5%E6%96%B0%E5%A2%9E%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • AngularJS是什么?AngularJs(后面就简称ng了)是一个用于设计动态web应用的结构框架。首先,它是...
    200813阅读 1,568评论 0 3
  • angular有哪些牛逼特性呢? 1、模板机制2、双向数据绑定3、模块4、指令5、依赖注入6、路由7、过滤器 An...
    Man僵小鱼阅读 1,135评论 0 6
  • ng-model 指令ng-model 指令 绑定 HTML 元素 到应用程序数据。ng-model 指令也可以:...
    壬万er阅读 857评论 0 2
  • AngularJS AngularJS概述 介绍 简称:ng Angular是一个MVC框架 其他前端框架: Vu...
    我爱开发阅读 2,327评论 0 8
  • 考完的第一天就发烧时种什么样的感觉! 昨天结束了期末考试,可是今天的我就发烧了! 上午起床感觉扁桃体发炎了,我吃了...
    1路向前阅读 147评论 0 0