简体   繁体   English

ngResource / $ resource注入不正确

[英]ngResource/$resource not injected correctly

The current task is to write a client for an API. 当前的任务是为API编写客户端。 The $resource module is not getting injected correctly. $ resource模块未正确注入。 I have injected ngResource to the module, and then also passed it into the factory declaration. 我已将ngResource注入模块,然后还将其传递到工厂声明中。 However inside the controller, if I try to console.log($resource), I get the following error: 但是在控制器内部,如果尝试使用console.log($ resource),则会出现以下错误:

TypeError: 'undefined' is not a function (evaluating '$resource('https://ourlink.nl',options)')

I am convinced the issue is happening/not happening in app.js, but receptive to input if you have other ideas. 我确信该问题在app.js中正在发生/未发生,但是如果您有其他想法可以接受。 Can you help me spot the problem? 你能帮我发现问题吗?

restClient.js restClient.js

function restClient ($resource) {
  'use strict';
  var options = {};

  init();

  return {
    endpoint: self.endpoint,
    sendRequest: sendRequest
  };

  function init () {
    self.endpoint = 'https://ourlink.nl';
  }

  function sendRequest () {
    $resource('https://ourlink.nl', options);
    return true;
  }

}

module.exports = restClient;

app.js app.js

var angular = require('angular');

// imports
require('angular-resource');

(function (angular, config, loaders, controllers, services, providers) {
      'use strict';

      angular
        .module('at.annabel', [
        'ngLocale',
        'ngRoute',
        'ngResource',
        'ngSanitize',
        'pascalprecht.translate',
        'ui.bootstrap'
      ])

    // constants
      .constant('translations', config.translations)

    // config
      .config([
        '$locationProvider',
        'config.BASE_PATH',
        providers.locationProvider
      ])

    // services
      .factory(
        'restClient',
        ['$resource', services.restClient]
      )

    // controllers
      .controller(
        'PlaceholderController',
        [controllers.PlaceholderController]
      )
    ;
}
        ))(
    angular,
    // config
    {
        menu: require('./config/menu').config
...
        }
    },
    // loaders
    {
        localeLoader: require('./config/locale-loader')
    },
    // controllers
    {
        PlaceholderController: require('./modules/placeholder/placeholder-controller')
    },
    // services
    {
        restClient: require('./services/restClient')
    },
    //providers
    {
        locationProvider: require('./config/location-provider')
    }
);

It turned out to be there the whole time. 事实证明它一直都在那里。 Since the app does not invoke restClient.js in the browser, I was only seeing the module invoked in test. 由于该应用程序未在浏览器中调用restClient.js,因此我仅看到测试中调用的模块。 The test runner wasn't properly set up for $resource, and $resource needed to be injected into the module by the test. 没有为$ resource正确设置测试运行程序,并且$ resource需要由测试注入到模块中。 Thanks to everyone who took the time to read and investigate. 感谢所有花时间阅读和调查的人。 I got caught up on the app.js file, because it is so unconventional, but turns out the whole file is legit. 我被app.js文件所困扰,因为它是如此的不常规,但事实证明整个文件都是合法的。

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

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