繁体   English   中英

AngularJS提供程序无法按照样式指南工作

[英]AngularJS provider not working as per styleguide

我正在遵循John Papa的Angular1样式指南,并尝试实现提供程序。

根据文档提供者如下所示:

angular
    .module('blocks.router')
    .provider('routerHelper', routerHelperProvider);

routerHelperProvider.$inject = ['$locationProvider', '$stateProvider', '$urlRouterProvider'];
/* @ngInject */
function routerHelperProvider($locationProvider, $stateProvider, $urlRouterProvider) {
    /* jshint validthis:true */
    this.$get = RouterHelper;

    $locationProvider.html5Mode(true);

    RouterHelper.$inject = ['$state'];
    /* @ngInject */
    function RouterHelper($state) {
        var hasOtherwise = false;

        var service = {
            configureStates: configureStates,
            getStates: getStates
        };

        return service;

        ///////////////

        function configureStates(states, otherwisePath) {
            states.forEach(function(state) {
                $stateProvider.state(state.state, state.config);
            });
            if (otherwisePath && !hasOtherwise) {
                hasOtherwise = true;
                $urlRouterProvider.otherwise(otherwisePath);
            }
        }

        function getStates() { return $state.get(); }
    }
}

在plnkr上模拟了一个非常基本的版本

我的提供者:

angular
  .module('plunker')
  .provider('random', function() {
    this.$get = helper;
    this.getX = function() {
      return 10;
    }

    function helper() {
      var provider = {
        getX: getX,
        getY: getY
      }

      return provider;

      function getX() {
        return 10;
      }

      function getY() {
        return 20;
      }
    }
  });

因此, getX可以正常工作,但是getY是未定义的。

恐惧,我缺少一些琐碎的东西。

看起来您有一个getX的吸气剂,但没有getY的吸气剂:

this.getX = function() {
  return 10;
}

this.getY = function() {
  return 20;
}

这是您的punker的分叉版本: http ://plnkr.co/edit/lHBj6LiirisZ4Bb6DM9M?p=preview

您是否也应该添加getY方法?

angular
  .module('plunker')
  .provider('random', function() {
    this.$get = helper;
    this.getX = function() {
      return 10;
    }
    this.getY = function() {
        return 10;
    }

    function helper() {
      var provider = {
        getX: getX,
        getY: getY
      }

      return provider;
      // not required after return statement, why you need them?
    }
});

暂无
暂无

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

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