user5443928
user5443928

Reputation:

Could not return value from factory method using Angular.js

I need some help. I am trying to return value rom actory method but could not do that getting some expected error. I am explaining my code below.

compliance.factory('showNabh',function($window,$timeout){
    var defaultView=function(){
        return true;
    }
    var auditView=function(){
        return false;
    }
})

compliance.controller('NABHParentController',function($scope,$http,$state,$window,$location,$filter,showNabh){
   $scope.showNabh=showNabh.defaultView();
})

Here I am getting the below error.

Error: [$injector:undef] http://errors.angularjs.org/1.4.6/$injector/undef?p0=showNabh
    at angularjs.js:6
    at Object.$get (angularjs.js:37)
    at Object.e [as invoke] (angularjs.js:39)
    at angularjs.js:41
    at d (angularjs.js:38)
    at e (angularjs.js:39)
    at Object.instantiate (angularjs.js:39)
    at angularjs.js:80
    at q (angularuirouter.js:7)
    at A (angularuirouter.js:7)

Here I need when I will call defaultView function I will get the true value and if I will call auditView unction I will get the false value. Actually i need to $scope.showNabh variable global so that I can assign any value to this variable in any controller. Please help.

Upvotes: 0

Views: 57

Answers (1)

Nicholas Tower
Nicholas Tower

Reputation: 84982

There's no return statement in your factory, so it's implicitly returning undefined. Try this:

compliance.factory('showNabh',function($window,$timeout){
    var defaultView=function(){
        return true;
    }
    var auditView=function(){
        return false;
    }

    return {
        defaultView: defaultView,
        auditView: auditView
    };
})

Upvotes: 1

Related Questions