admin管理员组文章数量:1397156
When calling a function like this:
HTML:
<select ng-model="var" ng-change="myFunction(var)">
<option ng-repeat="option in alloptions">{{option}}</option>
</select>
JS:
$scope.myFunction = function(){//do things}
it all works perfectly but when my function is within a service (which can be reached by the controller) the function doesn't get called. calling it by putting the service name as a prefix is also not an option (even though this works within javascript code itself) myService.myFunction(var)
So my question: what is the proper way to call services in ng-change when they are located in a service?
thanks in advance
When calling a function like this:
HTML:
<select ng-model="var" ng-change="myFunction(var)">
<option ng-repeat="option in alloptions">{{option}}</option>
</select>
JS:
$scope.myFunction = function(){//do things}
it all works perfectly but when my function is within a service (which can be reached by the controller) the function doesn't get called. calling it by putting the service name as a prefix is also not an option (even though this works within javascript code itself) myService.myFunction(var)
So my question: what is the proper way to call services in ng-change when they are located in a service?
thanks in advance
Share Improve this question asked Nov 18, 2013 at 17:32 SleeneeSleenee 5941 gold badge8 silver badges21 bronze badges2 Answers
Reset to default 29You 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);
}
Always remember that only properties defined in $scope is available in the UI and not your services.
While Whizkids answer is technically correct, I don't see a problem exposing the entire service to the view by attaching it directly to the scope. However this depends on the complexity of your project, and the design of your services.
For example, most services relating to reusable view states I write are simply a set of functions, with one exposing the underlying data.
return {
getData: function() { return container; },
doSomethingOnData: function() { /* some manipulation of container */ }
};
Now you can access the service data by attaching it simply to a $scope
variable:
$scope.serviceData = MyService.getData();
So your options for accessing service functions are either to go with Whizkids answer and create wrapping functions attached to scope, or to expose the service directly to the view.
$scope.$MyService = MyService;
To which you could do something like:
<div ng-click="$MyService.doSomethingOnData()"></div>
While this practice strictly goes against angulars separation principles, it does make the code concise if you have a lot of functions within your service you need to expose with simple wrappers.
本文标签: javascriptHow to call a service function in AngularJS ngclick (or ngchange)Stack Overflow
版权声明:本文标题:javascript - How to call a service function in AngularJS ng-click (or ng-change, ...)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737578948a1997402.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论