最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call function in ng-click which is defined in factory service - Stack Overflow

programmeradmin0浏览0评论

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>

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>
Share Improve this question asked Mar 11, 2014 at 7:12 Anshad VattapoyilAnshad Vattapoyil 23.5k19 gold badges90 silver badges134 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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

$scope.setTest=myFactory.setTest;

发布评论

评论列表(0)

  1. 暂无评论