關(guān)于 angularJS的一些用法
AngularJS
事件指令:
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綁定事件的
需要注意的是,使用事件對象的時候,需要在ng-click等指令里傳入$event,如:
<button ng-click="clickFn($event)" class="btn btn-danger">aa</button>
表單指令
ng-change
當(dāng)值發(fā)生改變的時候就會有用
有value值的一些個標(biāo)簽,能ng-model的,才能用喲
必須要ng-model配合使用
可以做數(shù)據(jù)驗證
ng-disabled 控制元素是否可用 ng-readonly ng-checked
控制checkbox是否被選中
只設(shè)置這個只能通過數(shù)據(jù)來控制是否選中
設(shè)置ng-model就可以通過它來控制數(shù)據(jù)
disabled和readonly的區(qū)別
表單元素都可以通過設(shè)置disabled或者readonly屬性對其禁用,disabled設(shè)置了之后,用戶不可以使用,并且表單不會提交該字段,readonly
僅是用戶禁用,也就是說,用戶不可操作,但是表單依然會提交
倒計時搶購小案例
$interval服務(wù)相當(dāng)于setInterval,可以自動進行臟數(shù)據(jù)檢驗
清除的話需要賦值然后$interval.cancel(timer)
ng-show 為true顯示。false隱藏
ng-hide 為true 隱藏。false 顯示
ng-if 和ng-show 一樣,只不過是如果不顯示的時候,節(jié)點不在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相關(guān)
ng-bind有一個問題,加上之后就不能在數(shù)據(jù)變量后面加別的東東了,這個標(biāo)簽里面只能顯示這條數(shù)據(jù),其他的就不行了比如
{{name}}---111 用ng-bind-template就好 ng-bind-template="{{name}}---111"
又有問題了,不能解析標(biāo)簽
沒事,用ng-bind-html
ng-bind-html="<h1>{{name}}---111</h1>"
這樣可不行哦,這是1.3前的,從1.3以后大換血的時候,為了精簡angular.js,把這個玩意給弄出去了,得用一個插件(模塊)
還得在angular.module里面給放進"ngSanitize"
然后需要把要顯示的標(biāo)簽掛在一個變量上,然后設(shè)置給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'
注意,因為其實內(nèi)部是ajax請求的,所以需要服務(wù)器環(huán)境下
ng-model-options='{updateOn:'blur'}'
綁定數(shù)據(jù)在顯示的過程中,內(nèi)部會一直操作節(jié)點,性能不好,可以這樣配置一下,在某個時刻去更新視圖顯示的數(shù)據(jù)就ok
AngularJS
ng-controller
可以用面向?qū)ο蟮乃季S來寫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"
再來說服務(wù),服務(wù)其實已經(jīng)說了很多了。
angularJS中,服務(wù)是用來通過某些功能
$http服務(wù)
能進行數(shù)據(jù)交互
$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 數(shù)據(jù)接口
params 提交的數(shù)據(jù) 相當(dāng)于$.ajax里的data:{}
success 成功回調(diào)
error 錯誤回調(diào)
這里要說下JSONP技術(shù)
JSONP是解決跨域問題的一種常見方式
跨域問題:因為瀏覽器有同源策略,所以當(dāng)不同域間進行數(shù)據(jù)交互的時候就會出現(xiàn)跨域問題
同源策略:只有在同協(xié)議,同域名,同端口的情況下才能進行數(shù)據(jù)交互
JSONP的原理:可以利用script標(biāo)簽(會使用回調(diào)函數(shù)來接收數(shù)據(jù))的src屬性不受同源策略的影響,可以請求到不同域的數(shù)據(jù),通過設(shè)置回調(diào)函
數(shù)來接收數(shù)據(jù)
JSONP是前后端結(jié)合的跨域方式:因為前端請求到數(shù)據(jù)后需要在回調(diào)函數(shù)中使用,所以后端得將數(shù)據(jù)放回到回調(diào)函數(shù)中
JSONP屬于AJAX嗎?ajax是指通過使用xmlhttprequest對象進行異步數(shù)據(jù)交互的技術(shù),jsonp是依靠scriptsrc屬性來獲取的,不屬于ajax
JSONP有什么缺點,使用的時候需要注意什么?
不能post跨域處理,需要注意的是:每次請求應(yīng)該動態(tài)的創(chuàng)建script標(biāo)簽和回調(diào)函數(shù),數(shù)據(jù)獲取完成后銷毀。
如果method是jsonp的話,就可以用jsonp去跨域請求,但是注意要在url后寫關(guān)于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 }) } })
$location服務(wù)
console.log($location.absUrl())//輸出絕對地址 console.log($location.host())//輸出域名 console.log($location.port())//輸出端口 console.log($location.protocol())//協(xié)議 $location.path("aaa")//在路由中控制切換頁面 console.log($location.path()) // #/aaa
$log 服務(wù)
多種控制臺輸出模式
$log.info("info"); $log.warn("warn"); $log.error("error"); $log.log("log");
angularJs對服務(wù)供應(yīng)商配置
例如
myapp.config(["$interpolateProvider",function($interpolateProvider){ $interpolateProvider.startSymbol("!!"); $interpolateProvider.endSymbol("!!"); }])
angular就不認識{{}}了,開始變成?。。?!
自定義服務(wù) 三種
1.factory
myapp.factory('serviceName',function(){ return .... })
可以return 字符串、數(shù)組、函數(shù)、對象(使用最多,最和邏輯)
引入方法和angualr自帶的前面加$的服務(wù)完全一樣,使用方法取決于return出來的是什么東西,自定義服務(wù)的服務(wù)名還是別加$了
eq:返回一個 兩個數(shù)之間的隨機數(shù)的服務(wù)
myapp.factory("myService",function(){ return { getRandom:function(a,b){ return Math.random()*(b-a)+a; } } })
自定義的服務(wù)可以依賴注入其他服務(wù)
myapp.factory('myHttpService',['$http',function($http){ return { $http({ url:...... }) } }])
eq:下一個自定義的http服務(wù)
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
可以通過去自定義一個服務(wù)供應(yīng)商去定義一個服務(wù),寫法有區(qū)別,服務(wù)功能性的東西需要嵌套一層返回
myapp. provider ('myHttpService',['$http',function($http){ return { $get:function(){ return:{//這里才是輸出 } } }])
外面return出來的是這個服務(wù)的供應(yīng)商,供應(yīng)商的$get方法里返回的才是供我們使用的部分,可以通過更改供應(yīng)商的部分參數(shù)來控制服務(wù)的功能,
eq:還是返回一個范圍內(nèi)的隨機數(shù),但是通過配置供應(yīng)商的一個值來控制服務(wù)返回的是整數(shù)還是小數(shù)
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; }])
通過這種方法創(chuàng)建的服務(wù)是可以配置供應(yīng)商的
3.service
通過這種方法創(chuàng)建出來的只能是對象
最簡單的創(chuàng)建方式,自帶返回,支持面向?qū)ο蟮膶懛?/p>
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; } }
多個控制器間數(shù)據(jù)的共享
實現(xiàn)多個控制器數(shù)據(jù)共享的方法有這樣三種,
第一種比較簡單,就是把數(shù)據(jù)放到父作用域上,就都可以訪問了
第二種就是在控制器里通過$$prevSibling找到兄弟作用域,然后使用數(shù)據(jù),需要注意的是,如果是初始數(shù)據(jù)類型的話就不能做數(shù)據(jù)雙向綁定了
第三種是定義服務(wù),把需要共享的數(shù)據(jù)做成服務(wù),這樣就都可以用了
<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>
自定義模塊
所有的模塊都有服務(wù),ng-app這個模塊理由¥scope什么的服務(wù),
咱們自己也可以寫一個模塊,然后里面可以去寫服務(wù)
這樣就可以把某些服務(wù)寫在某個自定義的模塊里,實現(xiàn)重復(fù)調(diào)用
例如把隨機數(shù)的例子寫在一個自定義的模塊里
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就是一個自定義模塊
總結(jié)
以上所述是小編給大家介紹的angularJS的一些用法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Angular4學(xué)習(xí)教程之DOM屬性綁定詳解
這篇文章主要給大家介紹了關(guān)于Angular4學(xué)習(xí)教程之DOM屬性綁定的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01Angular動態(tài)綁定樣式及改變UI框架樣式的方法小結(jié)
AngularJS 是一個 JavaScript 框架。它是一個以 JavaScript 編寫的庫。這篇文章主要介紹了Angular動態(tài)綁定樣式及改變UI框架樣式的方法小結(jié),需要的朋友可以參考下2018-09-09AngularJS 驗證碼60秒倒計時功能的實現(xiàn)
最近在做AngularJS 項目,這是寫的一個60秒倒計時功能,下面小編給大家介紹AngularJS 驗證碼60秒倒計時功能的實現(xiàn),需要的朋友參考下吧2017-06-06AngularJS入門教程二:在路由中傳遞參數(shù)的方法分析
這篇文章主要介紹了AngularJS在路由中傳遞參數(shù)的方法,結(jié)合實例形式分析了AngularJS實現(xiàn)路由中傳遞參數(shù)的相關(guān)技巧,并總結(jié)了相關(guān)操作步驟與注意事項,需要的朋友可以參考下2017-05-05仿Angular Bootstrap TimePicker創(chuàng)建分鐘數(shù)-秒數(shù)的輸入控件
這篇文章主要為大家詳細介紹了仿Angular Bootstrap TimePicker創(chuàng)建分鐘數(shù)-秒數(shù)的輸入控件的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07Angular-UI Bootstrap組件實現(xiàn)警報功能
這篇文章主要介紹了Angular-UI Bootstrap組件實現(xiàn)警報功能,對Angular.js services的學(xué)習(xí)有所幫助,需要的朋友可以參考下2018-07-07