【问题标题】:ngResource/$resource not injected correctlyngResource/$resource 未正确注入
【发布时间】:2015-11-17 18:59:10
【问题描述】:

当前任务是为 API 编写客户端。 $resource 模块未正确注入。我已经将 ngResource 注入到模块中,然后也将其传递到工厂声明中。但是在控制器内部,如果我尝试使用 console.log($resource),我会收到以下错误:

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

我确信问题在 app.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

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')
    }
);

【问题讨论】:

    标签: javascript angularjs dependency-injection ngresource


    【解决方案1】:

    原来它一直都在那里。由于应用程序没有在浏览器中调用 restClient.js,我只看到在测试中调用的模块。没有为 $resource 正确设置测试运行器,并且需要通过测试将 $resource 注入到模块中。感谢所有花时间阅读和调查的人。我发现了 app.js 文件,因为它非常规,但结果证明整个文件是合法的。

    【讨论】:

      猜你喜欢
      • 2016-02-27
      • 2017-01-11
      • 2016-12-10
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多