如何向链接函数注入服务?

Lui*_*lli 2 javascript angularjs leaflet angular-formly

我需要使用链接函数来操作 DOM 的某些元素,但为此我需要使用服务。在控制器功能中,我知道如何做到这一点,但在链接功能中我不知道如何实现这一点。

--编辑-- 让我说得更清楚:

我正在使用带有传单的角度形式。link但在传单中,事件必须按如下方式进行:

link: function($scope) {
  $scope.to.marker.on('dragend', function(eventArguments) {
    var newPosition = eventArguments.target.getLatLng();
    $scope.model.geocodes = newPosition;
  });
}
Run Code Online (Sandbox Code Playgroud)

问题是我需要为此注入服务。在 angular-formly (http://docs.angular-formly.com/docs/formlyconfig)中,它向我表明我可以在 上注入服务controller: function($scope),但不要给我一种方法来在link我上做同样的事情更改$scope.model.geocodes链接中的 ,它不会触发watcher我在custom-template.

我希望我已经说清楚了。

zer*_*298 5

我相信您可以将其注入指令并将其捕获到链接函数中:

function SomeDirective(SomeService){
    return{
        link:function(){
            SomeService.something();
        }
    }
}

SomeDirective.$inject = [
    "SomeService"
];

angular
    .module("some-module")
    .directive("some", SomeDirective);
Run Code Online (Sandbox Code Playgroud)