如何在工厂服务中定义的ng-click中调用函数

时间:2014-03-11 07:12:27

标签: javascript angularjs angularjs-directive angularjs-scope angular-services

如何在工厂服务中定义的ng-click中调用该函数,

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 个答案:

答案 0 :(得分:4)

应在您的范围内定义该方法,以使其可用于绑定。在您的控制器中,您可以

$scope.setTest=myFactory.setTest;