簡(jiǎn)介AngularJS中使用factory和service的方法
AngularJS支持使用服務(wù)的體系結(jié)構(gòu)“關(guān)注點(diǎn)分離”的概念。服務(wù)是JavaScript函數(shù),并負(fù)責(zé)只做一個(gè)特定的任務(wù)。這也使得他們即維護(hù)和測(cè)試的單獨(dú)實(shí)體??刂破?,過(guò)濾器可以調(diào)用它們作為需求的基礎(chǔ)。服務(wù)使用AngularJS的依賴(lài)注入機(jī)制注入正常。
AngularJS提供例如許多內(nèi)在的服務(wù),如:$http, $route, $window, $location等。每個(gè)服務(wù)負(fù)責(zé)例如一個(gè)特定的任務(wù),$http是用來(lái)創(chuàng)建AJAX調(diào)用,以獲得服務(wù)器的數(shù)據(jù)。 $route用來(lái)定義路由信息等。內(nèi)置的服務(wù)總是前綴$符號(hào)。
有兩種方法來(lái)創(chuàng)建服務(wù)。
- 工廠
- 服務(wù)
使用工廠方法
使用工廠方法,我們先定義一個(gè)工廠,然后分配方法給它。
var mainApp = angular.module("mainApp", []); mainApp.factory('MathService', function() { var factory = {}; factory.multiply = function(a, b) { return a * b } return factory; });
使用服務(wù)方法
使用服務(wù)的方法,我們定義了一個(gè)服務(wù),然后分配方法。還注入已經(jīng)可用的服務(wù)。
mainApp.service('CalcService', function(MathService){ this.square = function(a) { return MathService.multiply(a,a); } });
例子
下面的例子將展示上述所有指令。
testAngularJS.html
<html> <head> <title>Angular JS Forms</title> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app="mainApp" ng-controller="CalcController"> <p>Enter a number: <input type="number" ng-model="number" /> <button ng-click="square()">X<sup>2</sup></button> <p>Result: {{result}}</p> </div> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> <script> var mainApp = angular.module("mainApp", []); mainApp.factory('MathService', function() { var factory = {}; factory.multiply = function(a, b) { return a * b } return factory; }); mainApp.service('CalcService', function(MathService){ this.square = function(a) { return MathService.multiply(a,a); } }); mainApp.controller('CalcController', function($scope, CalcService) { $scope.square = function() { $scope.result = CalcService.square($scope.number); } }); </script> </body> </html>
結(jié)果
在Web瀏覽器打開(kāi)textAngularJS.html??吹浇Y(jié)果如下。
- AngularJS之自定義服務(wù)詳解(factory、service、provider)
- AngularJS自定義服務(wù)與fliter的混合使用
- Angularjs 自定義服務(wù)的三種方式(推薦)
- AngularJs自定義服務(wù)之實(shí)現(xiàn)簽名和加密
- 詳解AngularJS controller調(diào)用factory
- angularjs封裝$http為factory的方法
- angularJS Provider、factory、service詳解及實(shí)例代碼
- angularjs自定義ng-model標(biāo)簽的屬性
- 詳解AngularJS中自定義過(guò)濾器
- AngularJS創(chuàng)建自定義指令的方法詳解
- AngularJS基于factory創(chuàng)建自定義服務(wù)的方法詳解
相關(guān)文章
對(duì)angular 監(jiān)控?cái)?shù)據(jù)模型變化的事件方法$watch詳解
今天小編就為大家分享一篇對(duì)angular 監(jiān)控?cái)?shù)據(jù)模型變化的事件方法$watch詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10詳解在Angular4中使用ng2-baidu-map的方法
這篇文章主要介紹了在Angular4中使用ng2-baidu-map的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06AngularJS實(shí)現(xiàn)星星等級(jí)評(píng)分功能
這篇文章主要為大家詳細(xì)介紹了AngularJS實(shí)現(xiàn)星星等級(jí)評(píng)分功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Angular 與 Component store實(shí)踐示例
這篇文章主要為大家介紹了Angular 與 Component store實(shí)踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02詳解Angular系列之變化檢測(cè)(Change Detection)
這篇文章主要介紹了詳解Angular系列之變化檢測(cè)(Change Detection),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02使用 Github Actions 自動(dòng)部署 Angular 應(yīng)用到 Github Pages的方法
這篇文章主要介紹了使用 Github Actions 自動(dòng)部署 Angular 應(yīng)用到 Github Pages,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07angularjs的單選框+ng-repeat的實(shí)現(xiàn)方法
今天小編就為大家分享一篇angularjs的單選框+ng-repeat的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09