How to call a function in ng-click, which is defined in the factory service

How can I call a function in ng-click, which is defined inside the factory service,

app.factory('MyFactory', [function () {
    return {
        setTest: function(test) {
            alert(test);
        }
    };
}]);

app.controller('TestCtrl', ['$scope','MyFactory', function ($scope, MyFactory) {

}]);


<li><a href="#" ng-click="setTest('PHP')">PHP</a></li>
<li><a href="#" ng-click="setTest('Javascript')">Javascript</a></li>
+4
source share
1 answer

The method must be defined in your area so that it can be used in bindings. In your controller you can

$scope.setTest=myFactory.setTest;

+4
source

Source: https://habr.com/ru/post/1531114/


All Articles