简体   繁体   English

AngularAMD无法读取未定义的属性“ bootstrap”

[英]AngularAMD cannot read property 'bootstrap' of undefined

I'm trying to set up my angularjs app using AngularAMD (a Requirejs implementation with AngularJs), but sometimes i get 我正在尝试使用AngularAMD(带有AngularJs的Requirejs实现)设置我的angularjs应用程序,但是有时我得到

Cannot read property 'bootstrap' of undefined(…) 无法读取未定义的属性“ bootstrap”(…)

main.js main.js

 require.config({ paths: { //Angular 'angular': '/bower_components/angular/angular', 'angular-route': '/bower_components/angular-route/angular-route.min', 'angularAMD': '/bower_components/angularAMD/angularAMD.min', 'angular-slick': '/bower_components/angular-slick/dist/slick.min', 'angular-sanitize': '/bower_components/angular-sanitize/angular-sanitize', //Plugins 'jquery': '/bower_components/jquery/dist/jquery', 'materialize': '/bower_components/Materialize/dist/js/materialize.min', 'jquery-hammer': '/bower_components/Materialize/js/jquery.hammer', 'hammerjs': '/bower_components/Materialize/js/hammer.min', 'parallax': '/bower_components/parallax.js/parallax.min', 'slick': '/bower_components/slick-carousel/slick/slick.min', 'domReady': '/bower_components/requirejs/domReady', //Controllers 'HomeController': 'Components/Home/home.controller', }, shim: { angular: { exports: "angular", }, 'angular-route': ['angular'], 'angularAMD': ['angular'], 'angular-slick': ['angular', 'slick'], 'angular-sanitize': ['angular'], 'materialize': ['jquery', 'jquery-hammer', 'hammerjs'], 'parallax': ['jquery'], }, baseUrl: "src", deps: ['app'] }); }); 

app.js app.js

 define(['angularAMD', 'angular-route', 'angular-sanitize', 'angular-slick'], function(angularAMD) { var app = angular.module("app", ['ngRoute', 'ngSanitize', 'slick']); app.config(function($routeProvider, $locationProvider) { $routeProvider.when("/", angularAMD.route({ templateUrl: 'views/Home/home.html', controller: 'HomeController', controllerAs: 'vm' })); $locationProvider.html5Mode(true); }); return angularAMD.bootstrap(app); }); 

I've tried many differents ways, but none of them seems to work, what am i doing wrong?.. 我尝试了许多不同的方法,但是似乎都没有用,我在做什么错?

Note: it only happens sometimes, when the page loads.. 注意:仅在页面加载时才会发生。

Well since my last question was deleted, I was able to fix this issue. 好吧,由于我删除了上一个问题,因此能够解决此问题。 First: This maybe my neurosis, but add baseUrl before your paths array. 第一:这可能是我的神经症,但是在您的path数组之前添加baseUrl。

Second: (In your app.js), instead of 第二:(在您的app.js中),而不是

app.config(function($routeProvider, $locationProvider) {
    $routeProvider.when("/", angularAMD.route({
      templateUrl: 'views/Home/home.html',
      controller: 'HomeController',
      controllerAs: 'vm'
    }));
    $locationProvider.html5Mode(true);
  });
 return angularAMD.bootstrap(app);

Do this: 做这个:

var app = angular.module("app", ['ngRoute', 'ngSanitize', 'slick']);
app.init = function () {
   angular.bootstrap(document, ['app']);
};
return app;

This will load it to the DOM. 这会将其加载到DOM。 If you have several controllers do so like this. 如果您有多个控制器,请执行以下操作。

require.config({
  baseUrl: "src",
  paths: {
    //Angular
    'angular': '/bower_components/angular/angular',
    'angular-route': '/bower_components/angular-route/angular-route.min',
    'angularAMD': '/bower_components/angularAMD/angularAMD.min',
    'angular-slick': '/bower_components/angular-slick/dist/slick.min',
    'angular-sanitize': '/bower_components/angular-sanitize/angular-sanitize',

    //Plugins
    'jquery': '/bower_components/jquery/dist/jquery',
    'materialize': '/bower_components/Materialize/dist/js/materialize.min',
    'jquery-hammer': '/bower_components/Materialize/js/jquery.hammer',
    'hammerjs': '/bower_components/Materialize/js/hammer.min',
    'parallax': '/bower_components/parallax.js/parallax.min',
    'slick': '/bower_components/slick-carousel/slick/slick.min',
    'domReady': '/bower_components/requirejs/domReady',
  },
  shim: {
    jquery: { exports: '$' },
    angular: {
      exports: "angular",
    },
    'angular-route': {
      deps: ['angular'],
      exports: 'angular'
    },
    'angularAMD': {
      deps: ['angular'],
      exports: 'angular'
    },
    'angular-slick': ['angular', 'slick'],
    'angular-sanitize':  {
      deps: ['angular'],
      exports: 'angular'
    },
    'materialize': ['jquery', 'jquery-hammer', 'hammerjs'],
    'parallax': ['jquery'],
  },
  deps: ['app']
});
require(['app', 'other_controller', 'other_controller'], function(app) {
  app.init();
});

Lastly make sure, you do not have ng-app in your html. 最后请确保您的html中没有ng-app。

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

相关问题 无法使用Bootstrap Carousel读取未定义的属性'offsetWidth' - Cannot read property 'offsetWidth' of undefined with Bootstrap Carousel Bootstrap Carousel:无法读取未定义的属性'offsetWidth' - Bootstrap Carousel: Cannot read property 'offsetWidth' of undefined 无法读取未定义的Webpack Bootstrap React的属性“调用” - Cannot read property 'call' of undefined Webpack Bootstrap React 无法读取React Bootstrap Typeahead中未定义的属性'paginationOption' - Cannot read property 'paginationOption' of undefined in React Bootstrap Typeahead 引导自动完成错误:无法读取未定义的属性“” - Bootstrap Auto-complete Error: Cannot read property “ ” of undefined 无法使用 Bootstrap 轮播错误消息读取未定义的属性“offsetWidth” - Getting Cannot read property 'offsetWidth' of undefined with Bootstrap carousel error message Bootstrap Carousel:未捕获的类型错误:无法读取未定义的属性“offsetWidth” - Bootstrap Carousel: Uncaught TypeError: Cannot read property 'offsetWidth' of undefined 无法读取模态弹出窗口 Bootstrap 5.1 中未定义的属性“classList” - Cannot read property 'classList' of undefined in Modal popup Bootstrap 5.1 Webpack 4 + bootstrap [TypeError: Cannot read property 'jquery' of undefined] - Webpack 4 + bootstrap [TypeError: Cannot read property 'jquery' of undefined] 无法读取未定义的属性 - Cannot Read Property Undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM