AngularJS學習篇(八)

來源:互聯網
上載者:User

標籤:watch   service   char   app   log   ati   time   oct   body   

AngularJS 服務(Service)

在 AngularJS 中,服務是一個函數或對象,可在你的 AngularJS 應用中使用。

AngularJS 內建了30 多個服務。

為什麼使用服務?

在很多服務中,比如 $location 服務,它可以使用 DOM 中存在的對象,類似 window.location 對象

因為這些服務可以擷取到Angular應用聲明周期的每一個階段,並且和$watch整合,讓Angular可以監控應用,處理事件變化。普通的DOM對象則不能在Angular應用聲明周期中和應用整合。

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <script src="angular-1.6.3/angular.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl">    <div ng-app="myApp" ng-controller="myCtrl">        <p>現在時間是:</p>        <h1>{{theTime}}</h1>    </div>    <p>$interval 訪問在指定的周期(以毫秒計)來調用函數或計算運算式。</p></div><script>    var app = angular.module(‘myApp‘, []);    app.controller(‘myCtrl‘, function($scope,$interval) {        $scope.theTime = new Date().toLocaleTimeString();        $interval(function () {            $scope.theTime= new Date().toLocaleTimeString();        },1000);    });</script></body></html>
建立自訂服務

你可以建立訪問自訂服務,連結到你的模組中:

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <script src="angular-1.6.3/angular.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl">    <p>255 的16進位是:</p>    <h1>{{hex}}</h1></div><p>自訂服務,用於轉換16進位數:</p><script>    var app = angular.module(‘myApp‘, []);    app.service(‘heaxfy‘,function () {        this.myFunc = function (x) {            return x.toString(16);        }    });    app.controller(‘myCtrl‘, function($scope,heaxfy) {        $scope.hex = heaxfy.myFunc(255);    });</script></body></html>

$watch:持續監聽資料上的變化,更新介面,如:

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <script src="angular-1.6.3/angular.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl">    <b>請輸入姓名:</b><br>    <b>姓:</b><input type="text" ng-model="lastName"><br>    <b>名:</b><input type="text" ng-model="firstName"><br>    <h1>{{ lastName + " " + firstName }}</h1>    <h2>{{ fullName }}</h2></div><script>    var app = angular.module(‘myApp‘, []);    app.controller(‘myCtrl‘, function($scope) {        $scope.lastName = "";        $scope.firstName = "";        //監聽lastName的變化,更新fullName        $scope.$watch(‘lastName‘, function() {            $scope.fullName = $scope.lastName + " " + $scope.firstName;        });        //監聽firstName的變化,更新fullName        $scope.$watch(‘firstName‘, function() {            $scope.fullName = $scope.lastName + " " + $scope.firstName;        });    });</script></body></html>

 

AngularJS學習篇(八)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.