AngularJS 入门2-服务与$http

AngularJS 入门2-服务与$http

 

1.AngularJS 服务(Service)

AngularJS 中你可以创建自己的服务,或使用内建服务。

AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用。

AngularJS 内建了30 多个服务。

常用内建服务:

① $location 服务,它可以返回当前页面的 URL 地址。

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $location) {
    $scope.myUrl = $location.absUrl();
});

$http AngularJS 应用中最常用的服务。发送请求,接受数据。

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
    $http.get("welcome.htm").then(function (response) {
        $scope.myWelcome = response.data;
    });
});

③$timeout 服务,对应了 JS window.setTimeout 函数。

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
    $scope.myHeader = "Hello World!";
    $timeout( function () {
        $scope.myHeader = "How are you today?";
    } , 2000);
});

④$interval 服务,对应了 JS window.setInterval 函数。

每一秒显示信息:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
    $scope.theTime = new Date().toLocaleTimeString();
    $interval( function () {
        $scope.theTime = new Date().toLocaleTimeString();
    } , 1000);
});

创建自定义服务,服务就是一个函数

创建名为hexafy 服务:

app.service('hexafy', function() {
    this.myFunc = function (x) {
        return x.toString(16);
    }
});

使用自定义的的服务 hexafy 将一个数字转换为16进制数:

app.controller('myCtrl', function($scope, hexafy) {
    $scope.hex = hexafy.myFunc(255);
});

 

2.AngularJS $http服务

$http AngularJS 中的一个核心服务,用于发送请求并读取远程服务器的数据。

以下是存储在web服务器上的 JSON 文件:

http://www.runoob.com/try/angularjs/data/sites.php                                                                                                  

{

    "sites": [

        {

            "Name": "菜鸟教程",

            "Url": "www.runoob.com",

            "Country": "CN"

        },

        {

            "Name": "Google",

            "Url": "www.google.com",

            "Country": "USA"

        },

        {

            "Name": "Facebook",

            "Url": "www.facebook.com",

            "Country": "USA"

        },

        {

            "Name": "微博",

            "Url": "www.weibo.com",

            "Country": "CN"

        }

    ]

}

 

废弃声明 (v1.5)

v1.5 $http  success  error 方法已废弃。使用 then 方法替代。

 

AngularJS1.5 以上版本 - 通用方法 - 实例                                                                                                                     

var app = angular.module('myApp', []);

app.controller('siteCtrl', function($scope, $http) {

    $http({

        method: 'GET',

        url: 'https://www.runoob.com/try/angularjs/data/sites.php'

    }).then(function successCallback(response) {

            $scope.names = response.data.sites;

        }, function errorCallback(response) {

            // 请求失败执行代码

    });

});

AngularJS1.5 以上版本 - 简写方法 - 实例

var app = angular.module('myApp', []);

app.controller('siteCtrl', function($scope, $http) {

  $http.get("http://www.runoob.com/try/angularjs/data/sites.php")

  .then(function (response) {$scope.names = response.data.sites;});

});

AngularJS1.5 版本 - 简写方法 - 实例

var app = angular.module('myApp', []);

app.controller('siteCtrl', function($scope, $http) {

  $http.get("http://www.runoob.com/try/angularjs/data/sites.php")

  .success(function (response) {$scope.names = response.sites;});

});

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值