ng-click/dblclick ng-mousedown/up ng-mouseenter/leave ng-mousemove/over/out ng-keydown/up/press ng-focus/blur ng-submit
和ng-click同樣,都是給dom綁定事件的php
須要注意的是,使用事件對象的時候,須要在ng-click等指令裏傳入$event,如:html
<button ng-click="clickFn($event)" class="btn btn-danger">aa</button>
ng-change
當值發生改變的時候就會有用前端
有value值的一些個標籤,能ng-model的,才能用喲
必需要ng-model配合使用angularjs
能夠作數據驗證ajax
ng-disabled 控制元素是否可用 ng-readonly ng-checked 控制checkbox是否被選中 只設置這個只能經過數據來控制是否選中 設置ng-model就能夠經過它來控制數據
表單元素均可以經過設置disabled或者readonly屬性對其禁用,disabled設置了以後,用戶不可使用,而且表單不會提交該字段,readonlyjson
僅是用戶禁用,也就是說,用戶不可操做,可是表單依然會提交後端
$interval服務至關於setInterval,能夠自動進行髒數據檢驗
清除的話須要賦值而後$interval.cancel(timer)跨域
ng-show 爲true顯示。false隱藏數組
ng-hide 爲true 隱藏。false 顯示瀏覽器
ng-if 和ng-show 同樣,只不過是若是不顯示的時候,節點不在dom文檔中
var app = angular.module("myapp",[])
app.controller("myController",function ($scope,$interval) {
$scope.num=1
$scope.canBuy = false
$scope.time = 5
var timer = $interval(function () { $scope.time--; if($scope.time<=0){ $scope.canBuy=true $interval.cancel(timer) } },1000) })
ng-bind有一個問題,加上以後就不能在數據變量後面加別的東東了,這個標籤裏面只能顯示這條數據,其餘的就不行了好比
{{name}}---111
用ng-bind-template就好
ng-bind-template="{{name}}---111"
又有問題了,不能解析標籤
沒事,用ng-bind-html
ng-bind-html="<h1>{{name}}---111</h1>"
這樣可不行哦,這是1.3前的,從1.3之後大換血的時候,爲了精簡angular.js,把這個玩意給弄出去了,得用一個插件(模塊)
還得在angular.module裏面給放進"ngSanitize"
而後須要把要顯示的標籤掛在一個變量上,而後設置給ng-bind-html
$scope.text= "<h1>"+$scope.name+"---111</h1>" ng-bind-html=''text「
ng-non-bindable
這個指令可讓表達式不解析
<h3 ng-non-bindable>{{name}}</h3>
ng-include
能夠引入一個html代碼片斷,也須要變量來定義,代碼片斷裏也能夠寫表達式等
$scope.text='html/a.html'; ng-include='text'
注意,由於其實內部是ajax請求的,因此須要服務器環境下
ng-model-options='{updateOn:'blur'}'
綁定數據在顯示的過程當中,內部會一直操做節點,性能很差,能夠這樣配置一下,在某個時刻去更新視圖顯示的數據就ok
能夠用面向對象的思惟來寫controller
<div ng-controller="myController as myFun"> {{name}}<br> {{myFun.age}}<br> {{myFun.sex}} </div> myapp.controller("myController",["$scope",myFun]) function myFun($scope){ $scope.name='allen'; this.sex='male' } myFun.prototype.age="18"
angularJS中,服務是用來經過某些功能
能進行數據交互
$http({ url:"http://datainfo.duapp.com/shopdata/getclass.php", method:"get", params:{} }).success(function(data){ $scope.dataList=data; }).error(function(error){ console.log(error) })
method 表明傳遞方法 get、post
url 數據接口
params 提交的數據 至關於$.ajax裏的data:{}
success 成功回調
error 錯誤回調
JSONP是解決跨域問題的一種常見方式
跨域問題:由於瀏覽器有同源策略,因此當不一樣域間進行數據交互的時候就會出現跨域問題
同源策略:只有在同協議,同域名,同端口的狀況下才能進行數據交互
JSONP的原理:能夠利用script標籤(會使用回調函數來接收數據)的src屬性不受同源策略的影響,能夠請求到不一樣域的數據,經過設置回調函
數來接收數據
JSONP是先後端結合的跨域方式:由於前端請求到數據後須要在回調函數中使用,因此後端得將數據放回到回調函數中
JSONP屬於AJAX嗎?ajax是指經過使用xmlhttprequest對象進行異步數據交互的技術,jsonp是依靠scriptsrc屬性來獲取的,不屬於ajax
JSONP有什麼缺點,使用的時候須要注意什麼?
不能post跨域處理,須要注意的是:每次請求應該動態的建立script標籤和回調函數,數據獲取完成後銷燬。
若是method是jsonp的話,就能夠用jsonp去跨域請求,可是注意要在url後寫關於callback的值爲JSON_CALLBACK
百度搜索小例子
這裏引用的是 angular-sanitize.js
var app = angular.module("myapp",['ngSanitize'])
app.controller("myController",function ($scope,$http) {
$http({ url:"http://datainfo.duapp.com/shopdata/getclass.php", method:"post", params:{a:1} }).success(function (results) { $scope.dataList = results }).error(function (error) { console.log(error) }) }) app.controller("yourController",function ($scope,$http) { $scope.search = function () { $http({ url:"https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su", method:"jsonp", params:{ wd:$scope.wd, cb:'JSON_CALLBACK' } }).success(function (results) { $scope.dataList = results.s }) } })
console.log($location.absUrl())//輸出絕對地址 console.log($location.host())//輸出域名 console.log($location.port())//輸出端口 console.log($location.protocol())//協議
$location.path("aaa")//在路由中控制切換頁面 console.log($location.path()) // #/aaa
多種控制檯輸出模式 $log.info("info"); $log.warn("warn"); $log.error("error"); $log.log("log");
例如
myapp.config(["$interpolateProvider",function($interpolateProvider){ $interpolateProvider.startSymbol("!!"); $interpolateProvider.endSymbol("!!"); }])
angular就不認識{{}}了,開始變成!!!!
1.factory
myapp.factory('serviceName',function(){ return .... })
能夠return 字符串、數組、函數、對象(使用最多,最和邏輯)
引入方法和angualr自帶的前面加$的服務徹底同樣,使用方法取決於return出來的是什麼東西,自定義服務的服務名仍是別加$了
eq:返回一個 兩個數之間的隨機數的服務
myapp.factory("myService",function(){ return { getRandom:function(a,b){ return Math.random()*(b-a)+a; } } })
自定義的服務能夠依賴注入其餘服務
myapp.factory('myHttpService',['$http',function($http){ return { $http({ url:...... }) } }])
eq:下一個自定義的http服務
myapp.factory("myHttpService",["$http",function($http){ return { http:function(url,sfn,efn){ $http({ url:url, method:"get" }).success(sfn).error(efn) } } }])
myHttpService.http("http://datainfo.duapp.com/shopdata/getclass.php",function(data){ console.log(data) },function(data){ console.log(data) })
2.provider
能夠經過去自定義一個服務供應商去定義一個服務,寫法有區別,服務功能性的東西須要嵌套一層返回
myapp. provider ('myHttpService',['$http',function($http){ return { $get:function(){ return:{//這裏纔是輸出 } } }])
外面return出來的是這個服務的供應商,供應商的$get方法裏返回的纔是供咱們使用的部分,能夠經過更改供應商的部分參數來控制服務的功能
,
eq:仍是返回一個範圍內的隨機數,可是經過配置供應商的一個值來控制服務返回的是整數仍是小數
myapp.provider("myService",function(){ return { isInt:true, $get:function(){ var that=this; return { getRandom:function(a,b){ var num=Math.random()*(b-a+1)+a; if(that.isInt){ return Math.floor(num); }else{ return(num) } } } } } }) myapp.config(["myServiceProvider",function(myServiceProvider){ myServiceProvider.isInt=false; }])
經過這種方法建立的服務是能夠配置供應商的
3.service
經過這種方法建立出來的只能是對象
最簡單的建立方式,自帶返回,支持面向對象的寫法
myapp.service("myService",function(){ this.getRandom=function(a,b){ return Math.random()*(b-a)+a; } }) myapp.service("myService",aaa) function aaa(){ this.getRandom=function(a,b){ return Math.random()*(b-a)+a; } }
實現多個控制器數據共享的方法有這樣三種,
第一種比較簡單,就是把數據放到父做用域上,就均可以訪問了
第二種就是在控制器裏經過$$prevSibling找到兄弟做用域,而後使用數據,須要注意的是,若是是初始數據類型的話就不能作數據雙向綁定了
第三種是定義服務,把須要共享的數據作成服務,這樣就均可以用了
<body> <div class="container"> <div ng-controller="firstController"> <input type="text" class="form-control" ng-model="name"> <input type="text" class="form-control" ng-model="data.name"> <input type="text" class="form-control" ng-model="Data.name"> <p> first-name:{{name}}<br> first-data-name:{{data.name}}<br> first-Data-name:{{Data.name}} </p> </div> <div ng-controller="secondController"> <p> second-name:{{name}}<br> second-data-name:{{data.name}}<br> second-Data-name:{{Data.name}} </p> </div> </div> </body> <script src="../Base/angular.min.js"></script> <script> var app=angular.module("myapp",[]); app.factory("Data",function () { return { name:'lily' } }) app.controller("firstController",function ($scope,Data) { $scope.name="allen"; $scope.data={ name:'tom' } $scope.Data=Data; }) app.controller("secondController",function ($scope,Data) { $scope.name=$scope.$$prevSibling.name; $scope.data=$scope.$$prevSibling.data; $scope.Data=Data; }) </script>
全部的模塊都有服務,ng-app這個模塊理由¥scope什麼的服務,
我們本身也能夠寫一個模塊,而後裏面能夠去寫服務
這樣就能夠把某些服務寫在某個自定義的模塊裏,實現重複調用
例如把隨機數的例子寫在一個自定義的模塊裏
var myModule=angular.module("myModule",[]); myModule.service("myService",function(){ this.ran=function(a,b){ return Math.random()*(a+b)-a; } }) var myapp=angular.module("myapp",["myModule"]); myapp.controller("myController",["$scope","$log","myService",function($scope,$log,myService){ $log.log(myService.ran(5,10)) }])
其實像angualr.sanitize.js就是一個自定義模塊