AngularJS 服务(Service)

服务是一个函数或对象,可在你的 AngularJS 应用中使用
服务是作为一个参数传递到 controller 中。如果要使用它,需要在 controller 中定义
在很多服务中,比如 $location 服务,它可以使用 DOM 中存在的对象,类似 window.location 对象,但 window.location 对象在 AngularJS 应用中有一定的局限性。
AngularJS 会一直监控应用,处理事件变化, AngularJS 使用 $location 服务比使用 window.location 对象更好。

示例

<div ng-app="myApp" ng-controller="myCtrl">
    <p> 当前页面的url:</p>
    <h3>{{myUrl}}</h3>
</div>

<p>该实例使用了内建的 $location 服务获取当前页面的 URL。</p>
<script>
    var app = angular.module('myApp', []);
    //声明$location
    app.controller('myCtrl', function($scope, $location) {
        $scope.myUrl = $location.absUrl();
    });
</script>

常用服务

$http http://www.runoob.com/angularjs/angularjs-http.html

原文地址:https://www.cnblogs.com/Lulus/p/7873923.html