1

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

1
  • You could change your html to call searchFactory(search_name) instead of myfunc(search_name) since thats what you have mapped in your js "$scope.searchFactory = searchFactory.myfunc" Commented Jun 22, 2016 at 11:26

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

You need to store reference of searchFactory in $scope.searchFactory variable

$scope.searchFactory = searchFactory;

instead of

$scope.searchFactory = searchFactory.myfunc;

OR, Use set variable as myfunc in scope

$scope.myfunc = searchFactory.myfunc;

instead of

$scope.searchFactory = searchFactory.myfunc;

Comments

0

Instead of:

$scope.searchFactory = searchFactory.myfunc

Do something like:

$scope.myfunc = function(arg) {
    searchFactory.myfunc(arg);
}

Then, define search_name in ng-click="myfunc(search_name)" either as a model:

<input ng-model="search_name">

Or scope:

$scope.search_name = "Nicholas";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.