Zohair Zahid Zohair Zahid - 5 months ago 22
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');
return{
myfunc:function(search_name){
console.log('ok')
keyword_type = 1
WebSocketTest(search_name,keyword_type)

}
}
});

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

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>