0

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.

2
  • yes but still getting error. Commented Oct 24, 2017 at 14:37
  • Factory should return an object of functions Commented Oct 24, 2017 at 14:39

1 Answer 1

1

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

Comments

Your Answer

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