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
1 Answer
Reset to default 4The method should be defined on your scope for it to be usable in bindings. In your controller you can
$scope.setTest=myFactory.setTest;