4 回答
TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊
//defineamodulevarmainApp=angular.module("mainApp",[]);//createaservicewhichdefinesamethodsquaretoreturnsquareofanumber.mainApp.service('CalcService',function(MathService){this.square=function(a){returnMathService.multiply(a,a);}});//injecttheservice"CalcService"intothecontrollermainApp.controller('CalcController',function($scope,CalcService,defaultInput){$scope.number=defaultInput;$scope.result=CalcService.square($scope.number);$scope.square=function(){$scope.result=CalcService.square($scope.number);}});就是這樣調(diào)用service的
TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
Scope(作用域)是angular應(yīng)用程序中視圖(HTML)和控制器(JavaScript)之間的紐帶。
也就是說HTML和JavaScript可以通過它來傳輸、共享數(shù)據(jù)。Scope是一個(gè)對(duì)象,有自己的很多的方法和屬性,$scope正是創(chuàng)建控制器時(shí)傳遞的Scope對(duì)象的一個(gè)實(shí)例。
延伸知識(shí)點(diǎn):我們知道創(chuàng)建控制器時(shí)傳遞了Scope對(duì)象的一個(gè)實(shí)例,所以一個(gè)控制器對(duì)應(yīng)一個(gè)Scope對(duì)象,在多個(gè)控制器之間共享數(shù)據(jù)怎么辦?多個(gè)控制器若屬于同一個(gè)APP,我們可以用$rootScope來實(shí)現(xiàn)控制器間的數(shù)據(jù)共享
- 4 回答
- 0 關(guān)注
- 1241 瀏覽
添加回答
舉報(bào)
