美文网首页
AngularJS $http模块

AngularJS $http模块

作者: what__ | 来源:发表于2017-07-29 10:55 被阅读0次

正常还有一组数据 供测试使用测试时需要在本地环境下

<!DOCTYPE html>
 <html class="no-js" ng-app="myApp">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="./angular.min.js"> </script>
    </head>
    <body >
        <div ng-controller="myController">
            <ul>
                <li ng-repeat="item in items" > 
                    <span>{{item.name}}</span>
                    --
                    <span>{{item.price}}</span>
                </li>
            </ul>
        </div>
    </body>
    <script>
        //angular-resource(发送网络请求的与$http有相同功能,但是你更强大)依赖于ngResource模块  RESTful
        var app = angular.module("myApp",[])
        app.controller("myController",["$scope","$http",function($scope,$http){
            $scope.items=[]
            //$http 是angularjs 内部自己封装的ajax
            //$http.get()  返回一个pomise 对象
            //success和error 只是语法糖, 是对then() 封装
            //success()和then()的对比  success()只是返回的数据对象 而then()会原样的输出返回值
            $http.get("./data.json").success(function(res){
                console.log(res)
            });
            $http.get("./data.json").then(function(res){
                console.log(res)
            })
        }])
    </script>
</html>```

相关文章

网友评论

      本文标题:AngularJS $http模块

      本文链接:https://www.haomeiwen.com/subject/eacikxtx.html