angular的跨域(angular百度下拉提示類比)和angular選項卡

來源:互聯網
上載者:User

標籤:char   min   click   llb   border   oct   掛載   erro   height   

1.angular中$http的服務:

  $http.get(url,{params:{參數}}).success().error();

  $http.post(url,{params:{參數}}).success().error();

  $http.jsonp(url,{params:{wd:‘‘,cb:‘JSON_CALLBACK‘}}).success().error();

  注意jsonp中cb在angular中規定只能使用JSON_CALLBAC

 

  $watch(誰,做什麼,false);

  誰指的是需要監聽誰,做什麼指的是在監聽對象發生改變的時候會觸發的函數,最後一個參數布爾值,true表示深度監視,比如監聽的是對象arr,在arr中的每一項發生改變的時候都會觸發函數,true還表示可以監聽多個,如果預設false的話,只能監聽單個對象。

 

下面使用angular的jsonp來做了一個簡單百度搜尋下拉,原生的百度搜尋下來再前面的文章中寫過,有興趣的可以搜一下看看

<!doctype html><html ng-app="text"><head><meta charset="utf-8"><title>angular百度下拉搜尋</title><script src="angular.min.js"></script><script>var app=angular.module(‘text‘,[]);app.controller(‘cont‘,[‘$scope‘,‘$http‘,function($scope,$http){$scope.arr=[];//提前初始化資料,這裡給一個初始值,否則重新整理頁面angular的髒檢查會認為是失敗$scope.str=‘‘;//注意在angular中不能使用angular以外的方法,例如這裡使用jquery的$.ajax或者$.getScript會出現問題$scope.$watch(‘str‘,function(){$http.jsonp(‘https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su‘,{params:{wd:$scope.str,//angular規定cb必須是用"JSON_CALLBACK"cb:‘JSON_CALLBACK‘}}).success(function(json){$scope.arr=json.s;}).error(function(){alert(‘失敗了‘);});});}]);</script></head><body><div ng-controller="cont">    <input type="text" ng-model="str"/>        <ul>        <li ng-repeat="s in arr">{{s}}</li>        </ul>    </div></body></html>

  

 

2.angular的選項卡,自己認為選項卡雖然簡單,但是透過選項卡能展現很多東西的思路,這個angular的選項卡還是很簡單的

<!doctype html><html ng-app="text"><head><meta charset="utf-8"><title>angular選項卡</title><script src="angular.js"></script><style>*{padding:0;margin:0;}p{width:100px;height:100px;background:#ccc;border:1px solid #000;}.active{background:skyblue;}input{background:#333;color:#fff;}</style><script>var app=angular.module(‘text‘,[]);app.controller(‘cont1‘,[‘$scope‘,function($scope){//利用$scope.now來控制div的顯示和隱藏$scope.now=0;$scope.arr=[{name:"音樂",content:"BEYOND不朽"},{name:"體育",content:"C羅是垃圾"},{name:"新聞",content:"物價還在上漲"},];//掛載一個函數過渡一下,解決ng-repeat和ng-事件同時出現的問題$scope.fn=function(n){$scope.now=n;};}]);</script></head><body><div ng-controller="cont1"><!--注意在angular中ng-repeat和ng-事件同時出現會出現問題,解決辦法是在$scope下掛載的函數中過渡一下--><input ng-repeat="json in arr" type="button" value="{{json.name}}" ng-class="now==$index?‘active‘:‘‘" ng-click="fn($index)" />    <p ng-repeat="json in arr" ng-show="now==$index">{{json.content}}</p> </div></body></html>

  

 

angular的跨域(angular百度下拉提示類比)和angular選項卡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.