Zohair Zahid Zohair Zahid - 1 year ago 57
Javascript Question

AngularJS : Calling function in factory from html ng-click

I am trying to call a fucntion(myfunc), which is declared in Factory, from html :

<button type="button" class="btn btn-default btn-sm" ng-click="myfunc(search_name)">Search</button>

The controller and the factory code are as follows:

var angularjsapp = angular.module('graphApp', ['ngAnimate', 'ui.bootstrap']);
angularjsapp.factory('searchFactory', function() {
//return $resource('friends.json');
keyword_type = 1


angularjsapp.controller('AccordionDemoCtrl', function($scope,searchFactory) {
$scope.count = 0;
$scope.namesPerPage = 10
$scope.currentPage = 1;
$scope.searchFactory = searchFactory.myfunc

So far I am unable to log 'ok' in the console

Answer Source

You should call the scope function assigned to searchFactory.myfunc like so:

<button type="button" class="btn btn-default btn-sm" ng-click="searchFactory(search_name)">Search</button>