Javascript 安装 Angular Material,即使我使用的是 1.3.0 版,“无法实例化模块 ngMaterial”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31032714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:05:56  来源:igfitidea点击:

Installing Angular Material, "Failed to instantiate module ngMaterial" even though I'm using angular version 1.3.0

javascriptangularjsangular-material

提问by Daniel Kobe

This is very frustrating, Im using a mean.js yeoman generated application and can't get to seem to get angular-material up and running. I read another stackoverflow question about the issue Angularjs with material design Failed to instantiate module ngMaterial. So I updated my bower.json file and ran a bower updateand bower install. It still doesnt work. During the bower update, bower gave me this message Unable to find a suitable version for angular, please choose one:, but I always chose an Angular version 1.3.0 or greater.
Heres a copy of my bower.json:

这非常令人沮丧,我使用的是 mean.js yeoman 生成的应用程序,但似乎无法启动和运行 angular-material。我阅读了另一个关于Angularjs with material design Failed to instanceiate module ngMaterial问题的stackoverflow问题。所以我更新了我的 bower.json 文件并运行了一个bower updatebower install. 它仍然不起作用。在 bower 更新期间,bower 给了我这个消息Unable to find a suitable version for angular, please choose one:,但我总是选择 Angular 1.3.0 或更高版本。
这是我的 bower.json 的副本:

{
  "name": "colign",
  "version": "0.0.1",
  "description": "Full-Stack JavaScript with MongoDB, Express, AngularJS, and Node.js",
  "dependencies": {
    "bootstrap": "~3",
    "angular": "~1.3",
    "angular-resource": "~1.3",
    "angular-mocks": "~1.3",
    "angular-bootstrap": "~0.11.2",
    "angular-ui-utils": "~0.1.1",
    "angular-ui-router": "~0.2.11",
    "angular-material": "~0.10.0"
  }
}

Heres the console error message: Error: [$injector:modulerr] Failed to instantiate module ngMaterial due to: Error: [$injector:nomod] Module 'ngMaterial' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

这是控制台错误消息:错误:[$injector:modulerr] 无法实例化模块 ngMaterial 由于:错误:[$injector:nomod] 模块 'ngMaterial' 不可用!您拼错了模块名称或忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。

Angular config.js file:

角度 config.js 文件:

'use strict';

// Init the application configuration module for AngularJS application
var ApplicationConfiguration = (function() {
    // Init module configuration options
    var applicationModuleName = 'colign';
    var applicationModuleVendorDependencies = ['ngResource', 'ui.router', 'ui.bootstrap', 'ui.utils', 'ngMaterial'];

    // Add a new vertical module
    var registerModule = function(moduleName, dependencies) {
        // Create angular module
        angular.module(moduleName, dependencies || []);

        // Add the module to the AngularJS configuration file
        angular.module(applicationModuleName).requires.push(moduleName);
    };

    return {
        applicationModuleName: applicationModuleName,
        applicationModuleVendorDependencies: applicationModuleVendorDependencies,
        registerModule: registerModule
    };
})();

Thanks in advance for any help!

在此先感谢您的帮助!

回答by PSL

Angular material, ngMaterial, has dependency on ngAriaand ngAnimateas well. You would need to load them.

角材料 ,ngMaterial也依赖于ngAriangAnimate。您需要加载它们。

angular.module('ngMaterial', ["ng","ngAnimate","ngAria", ...other material sub modules

You could download it from angular ariaand angular animate. Use the correct version of these based on your angular version.

您可以从angular ariaangular animate下载它。根据您的角度版本使用这些的正确版本。

Also just adding the scripts in the project is not enough you need to load them in your html as well. Also load them before ng-materialscript.

此外,仅在项目中添加脚本是不够的,您还需要在 html 中加载它们。在ng-material脚本之前加载它们。