angularjs中使用$q.defer

方法method1和方法method2的区别,我还正在研究中。。。待添加

代码如下:

<html ng-app="myApp">
<head>
<title>angularjs-ajax</title>
<script type="text/javascript" src="angular.min.js" charset="utf-8"></script>
</head>
<body ng-controller="ctrl">
<input type="button" value="抓取页面内容1" ng-click="method1()" /> 
<input type="button" value="抓取页面内容2" ng-click="method2()" /> 
<div style="border: 1px solid #ccc;width: 500px;height:400px;">{{content}}</div>
<script>
    var app = angular.module('myApp',[]);
    app.controller("ctrl",function($scope,$http,$q){
        
        $scope.method1 = function() {
            $http.get('a1.htm').success(function (data) {
                $scope.content = data;
            });
        };
        
        $scope.method2 = function() {
            var deferred = $q.defer();
            $http.get("a1.htm").success(function (data) {
                $scope.content = data;
                deferred.resolve(response);
            }).error(function(error){
                alert(123);
                deferred.reject(error);
            });
        };
    });
    </script>    
</body>
</html>

 

posted @ 2016-09-13 16:01  魔豆  阅读(590)  评论(0编辑  收藏  举报