angular学习笔记01

angular.js路由功能

用于实现单页应用

//html 代码
<div ng-view></div>

//js代码
angular.module('myM1',['ng','ngRoute'])
.config(['$routeProvider',function($routeProvider){
$routeProvider
.when('/',{template:'这是首页页面'})
.when('/computers',{template:'这是电脑分类页面'})
.when('/printers',{templateUrl:'printers.html'})
.otherwise({redirectTo:'/'});
}])

 angular.js自定义指令

app.directive('runoobDirective',function(){
                return {
                    template:'<span>这个是自定义指令!</span>'
                };
            })

angular.js包含外部html文件

<div ng-include="'myUsers_List.htm'"></div>

angular.js   ng-app指令意义

<html ng-app>

angularjs启动生成视图时,会根据ng-app元素同$rootScope进行绑定。$rootScope是所有$scope对象的最上层。
$rootScope相当于JavaScript的全局,使用频繁使用$rootscope是不太好的,相当于污染了全局。

指令通常不会自己创建$scope,但也有例外,例如ng-controller,ng-repeat 指令会创建自己的子作用域,并将它们附加到DOM元素上。

angular.js自定义指令,模块

///自定义指令,或组件
<my-directive> </my-directive>
    <div my-directive></div>
    <div class="my-directive"></div>

app.directive('myDirective',function(){
                return {
                    restrict:'EAC',//element , attr(推荐) , class ,m->注释
                    replace:true,//全部都用template替换
                    template:'<a href="http://www.baidu.com">click me </a>'
                };
            })

angular.js动态生成并双向数据绑定

//模板生成并双向绑定
<label for="">Their URL field: </label>
    <input type="text" ng-model="theirUrl"/>
<div my-directive
            some-attr = 'theirUrl'
            my-link-text = 'click me to go to google'
            ></div>



.directive('myDirective',function(){
                return {
                    restrict:'EAC',//element , attr(推荐) , class ,m->注释
                    replace:true,//全部都用template替换
                    scope:{
                        myUrl: '=someAttr',
                        myLinkText: '@'
                    },
                    template:'<div><label>My Url Field:</label> <input type="text" ng-model="myUrl"/>
                    <a href="{{myUrl}}">{{myLinkText}}</a></div>'
                };
            })

全局rootscope如何访问

//在angulard代码中可以使用run方法来访问$rootscope

app.run(function($rootScope){
$rootScope.rootScope = 'hello word'
})

//ng-if 和ng-show ,ng-hide 的区别是ng-if会真正的移除或生成节点。ng-show 是通过css来显示或隐藏节点。

//ng-repeat 遍历集合
   会暴露一些特殊的属性
$index 遍历到第几个值 //值是从0 开始到length-1 ,所以奇数02468,偶数135
$first 遍历到第一个值的时候为true
$middle 遍历处于第一个与最后一个之间的值是为true
$last 遍历到最后一个值的时候为true
$even $index为偶数的时候为true
$odd $index为奇数的时候为true

//ng-init 指令用来设置内部作用域的初始状态.

//ng-cloak 也可以避免闪烁
<p ng-cloak>{{name}}</p>

//ng-bind-template,用于绑定多个表达式
<p ng-bind-template="{{message}}{{name}}">

//ng-change//输入的值发生改变时触发的事件
<input type="text" ng-model="equation" ng-change="change()"/>
$scope.change= function(){
console.log($scope.equation)
}

//directive 自定义指令的全部属性有


restrict:  指定angular指令在dom中以哪种方式声明,默认为a(attr),e(ele),c(class),m(comments),
可以同时指定多个 restrict:'EAMC'

priority:  优先级,数字类型 默认为零 (优先级最高的指令是ng-repeat)

terminal: 布尔值.比本指令优先级低的指令会停止。同一等级的指令不会被影响

template:  字符串或函数,可选,必须有一个(htm文本)或(可以接受两个参数的函数(temement,tattrs))
注意1,如果有多个同等级的标签,必须要有一根标签包含全部的标签
注意2,如果要换行,注意在每一行的后面加上反斜线(推荐使用templateUrl来引入外部的模板)

templateUrl: 可选参数1.html文件路径的字符串.2.(可以接受两个参数的函数(temement,tattrs))


replace:  布尔值 默认为false.模板默认插入到自定义指令的内部:为true则是直接插入

还有一些属性就等下篇。

原文地址:https://www.cnblogs.com/webHero/p/5751237.html