AngularJs-服务中的功能如何相互访问

AngularJs - how do functions within a service access each other?

本文关键字:何相互 访问 功能 服务 AngularJs-      更新时间:2023-09-26

我有以下javascript:

var myApp = angular.module('myApp', []);
myApp.factory( "TestService", function() {
    return {
        clickString: "click me",   
        clickStringProxy: this.clickString
    }
});
function MyCtrl($scope, TestService) {
    $scope.clickString = TestService.clickString;
    $scope.clickStringProxy = TestService.clickStringProxy;
}

以上是我尝试使TestService.clickStringProxy访问TestService.clickString(通过基本的"this")时失败的尝试。

Js报价:http://jsfiddle.net/eDb2S/96/

如何允许服务中的功能相互访问?

我试过各种说法来补救这种情况,但都无济于事。

声明服务对象操作它并返回。

myApp.factory( "TestService", function() {
    var service=  {
        clickString: "click me"
    }
    service.clickStringProxy = service.clickString;
    return service;
});

http://jsfiddle.net/eDb2S/97/