如何调用AngularJS NG单击服务功能(或NG-变化,...)? [英] How to call a service function in AngularJS ng-click (or ng-change, ...)?

查看:142
本文介绍了如何调用AngularJS NG单击服务功能(或NG-变化,...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当调用这样的功能:

HTML

<select ng-model="var" ng-change="myFunction(var)">
  <option ng-repeat="option in alloptions">{{option}}</option>
</select>

JS:

$scope.myFunction = function(){//do things}

这一切完美的作品,但是当我的职责是服务(可通过控制器来达到)的功能不会被调用内。通过将服务名称为preFIX调用它也不是一个选项(即使这个工程的javascript code本身内) myService.myFunction(VAR)

所以我的问题:什么是他们位于一个服务调用服务NG-变化的正确方法

So my question: what is the proper way to call services in ng-change when they are located in a service?

在此先感谢

推荐答案

您需要调用使用函数通过范围服务功能。因此,在你的榜样,你可以调用范围的MyFunction的方法内的服务方法,提供您的服务到您的控制器注入。

You need to call service functions using functions through scope. So in your example, you can call the service method inside the scope's myfunction method, provided your service is injected in to your controller.

$scope.myFunction = function(var){

   myService.myFunction(var);    
}

永远记住,只有在$范围定义的属性可在UI,而不是你的服务。

Always remember that only properties defined in $scope is available in the UI and not your services.

这篇关于如何调用AngularJS NG单击服务功能(或NG-变化,...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆