How to call function in ng-click which is defined

2019-06-27 13:44发布

问题:

How can I call the function in ng-click which is defined inside a 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>

回答1:

The method should be defined on your scope for it to be usable in bindings. In your controller you can

$scope.setTest=myFactory.setTest;