简体   繁体   English

无法使用Angular.js从工厂方法返回值

[英]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. 我正在尝试返回rom actory方法的值,但无法做到这一点,得到了一些预期的错误。 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. 在这里,我需要在调用defaultView函数时获取true值,并且如果我将调用auditView函数获取虚假值。 Actually i need to $scope.showNabh variable global so that I can assign any value to this variable in any controller. 实际上,我需要全局$scope.showNabh变量,以便可以在任何控制器中将此变量分配任何值。 Please help. 请帮忙。

There's no return statement in your factory, so it's implicitly returning undefined. 您的工厂中没有return语句,因此它隐式返回undefined。 Try this: 尝试这个:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM