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

How can I call the function in ng-click which is defined inside a factory service,app.factory('MyF

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744788928a4593794.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信