<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="angular.min.js"></script> <script> angularjs是独立的框架,可以不用jquery. ng的服务: --$scope --$watch :scope的子服务 --$apply :scope的子服务 --$rootScope --$timeout --$interval --$filter --$http :类似于ajax,这是angularjs自带的请求方式不同于jquery的请求。 var m1 = angular.module('myApp',[]); m1.controller('Aaa',['$scope','$http',function($scope,$http){ $http({ method : 'GET', url : 'data.php' }).success(function(data,state,headers,config){ console.log(state);//请求成功返回200状态 }).error(function(data){ console.log(data); }); $http.get('data.php').success(function(data,state,headers,config){ console.log(data); }); }]); </script> </head> <body> <div ng-controller="Aaa"> </div> </body> </html>
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="angular.min.js"></script> <script> var m1 = angular.module('myApp',[]); m1.controller('Aaa',['$scope','$http','$timeout',function($scope,$http,$timeout){ var timer = null; $scope.data = []; $scope.change = function(name){ $timeout.cancel(timer); timer = $timeout(function(){ $http({ method : 'JSONP', url : 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd='+name+'&cb=JSON_CALLBACK' }).success(function(data){ //console.log(data); $scope.data = data.s; }); },500); }; }]); </script> </head> <body> <div ng-controller="Aaa"> <input type="text" ng-model="name" ng-keyup="change(name)"> <input type="button" ng-click="change(name)" value="搜索"> <ul> <li ng-repeat="d in data">{{d}}</li> </ul> </div> </body> </html>
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5742029.html,如需转载请自行联系原作者