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

时间:2016-05-22 03:57:47

标签: javascript angularjs requirejs angular-amd

我试图使用AngularAMD设置我的angularjs应用程序(使用AngularJs的Requirejs实现),但有时我得到

  

无法读取属性' bootstrap'未定义的(...)

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



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

});




我尝试了许多不同的方法,但它们似乎都没有用,我做错了什么?...

注意:只有在页面加载时才会发生..

1 个答案:

答案 0 :(得分:-1)

自从我的上一个问题被删除以来,我就能解决这个问题了。第一:这可能是我的神经症,但在路径数组之前添加baseUrl。

第二:(在你的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);

这样做:

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

这会将它加载到DOM。如果你有几个控制器这样做。

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

最后请确保您的html中没有ng-app。