angularjs - 异步加载 AngularUI Bootstrap

标签 angularjs angular-ui angular-ui-bootstrap

现在一切都非常“新鲜”。我已经有了 Angular 种子项目,并且正在使用异步索引页面。当我添加 Angular-Bootstrap 的引用时,它爆炸了。当我不加载异步样式时,不会发生这种情况。

如何将 AngularUI Bootstrap 与 $script 加载器一起使用?

我遇到的错误是:

Uncaught TypeError: Object #<Object> has no method 'controller' ui-bootstrap-0.2.0.js:9
(anonymous function) ui-bootstrap-0.2.0.js:9

Uncaught Error: No module: ui.bootstrap.collapse new-job-order:18
(anonymous function) new-job-order:18
d new-job-order:18
(anonymous function) new-job-order:18
(anonymous function) angular.js:2754
forEach angular.js:133
loadModules angular.js:2750
(anonymous function) angular.js:2755
forEach angular.js:133
loadModules angular.js:2750
(anonymous function) angular.js:2755
forEach angular.js:133
loadModules angular.js:2750
(anonymous function) angular.js:2755
forEach angular.js:133
loadModules angular.js:2750
createInjector angular.js:2692
bootstrap angular.js:959
(anonymous function) new-job-order:45
o new-job-order:29
e.onload.e.onerror.e.(anonymous function) new-job-order:29

更新

我的代码实际上只是种子模板,使用index-async.html 文件。然后我添加(或尝试)bootstrap-ui。相关部分是:

  <script>
    // include angular loader, which allows the files to load in any order
    /*
     AngularJS v1.0.0rc1
     (c) 2010-2012 AngularJS http://angularjs.org
     License: MIT
    */
    'use strict';(function(i){function d(c,a,e){return c[a]||(c[a]=e())}return d(d(i,"angular",Object),"module",function(){var c={};return function(a,e,f){e&&c.hasOwnProperty(a)&&(c[a]=null);return d(c,a,function(){function b(a,b,d){return function(){c[d||"push"]([a,b,arguments]);return g}}if(!e)throw Error("No module: "+a);var c=[],d=[],h=b("$injector","invoke"),g={_invokeQueue:c,_runBlocks:d,requires:e,name:a,provider:b("$provide","provider"),factory:b("$provide","factory"),service:b("$provide","service"),
    value:b("$provide","value"),constant:b("$provide","constant","unshift"),filter:b("$filterProvider","register"),directive:b("$compileProvider","directive"),config:h,run:function(a){d.push(a);return this}};f&&h(f);return g})}})})(window);

    // include a third-party async loader library
    /*!
     * $script.js v1.3
     * https://github.com/ded/script.js
     * Copyright: @ded & @fat - Dustin Diaz, Jacob Thornton 2011
     * Follow our software http://twitter.com/dedfat
     * License: MIT
     */
    !function(a,b,c){function t(a,c){var e=b.createElement("script"),f=j;e.onload=e.onerror=e[o]=function(){e[m]&&!/^c|loade/.test(e[m])||f||(e.onload=e[o]=null,f=1,c())},e.async=1,e.src=a,d.insertBefore(e,d.firstChild)}function q(a,b){p(a,function(a){return!b(a)})}var d=b.getElementsByTagName("head")[0],e={},f={},g={},h={},i="string",j=!1,k="push",l="DOMContentLoaded",m="readyState",n="addEventListener",o="onreadystatechange",p=function(a,b){for(var c=0,d=a.length;c<d;++c)if(!b(a[c]))return j;return 1};!b[m]&&b[n]&&(b[n](l,function r(){b.removeEventListener(l,r,j),b[m]="complete"},j),b[m]="loading");var s=function(a,b,d){function o(){if(!--m){e[l]=1,j&&j();for(var a in g)p(a.split("|"),n)&&!q(g[a],n)&&(g[a]=[])}}function n(a){return a.call?a():e[a]}a=a[k]?a:[a];var i=b&&b.call,j=i?b:d,l=i?a.join(""):b,m=a.length;c(function(){q(a,function(a){h[a]?(l&&(f[l]=1),o()):(h[a]=1,l&&(f[l]=1),t(s.path?s.path+a+".js":a,o))})},0);return s};s.get=t,s.ready=function(a,b,c){a=a[k]?a:[a];var d=[];!q(a,function(a){e[a]||d[k](a)})&&p(a,function(a){return e[a]})?b():!function(a){g[a]=g[a]||[],g[a][k](b),c&&c(d)}(a.join("|"));return s};var u=a.$script;s.noConflict=function(){a.$script=u;return this},typeof module!="undefined"&&module.exports?module.exports=s:a.$script=s}(this,document,setTimeout)

    // load all of the dependencies asynchronously.
    $script([
      'lib/angular/angular.js',
      'lib/angular/angular-resource.js',
      'lib/angular-ui/ui-bootstrap-0.2.0.js',
      'js/app.js',
      'js/services.js',
      'js/controllers.js',
      'js/filters.js',
      'js/directives.js'
    ], function() {
      // when all is done, execute bootstrap angular application
      angular.bootstrap(document, ['myApp']);
    });
  </script>

然后在 app.js 中我这样做:

angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'ngResource', 'ui.bootstrap']).
  config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: MyCtrl1});
    $routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: MyCtrl2});
    $routeProvider.otherwise({redirectTo: '/view1'});
    $locationProvider.html5Mode(true).hashPrefix('!');
  }]);

最佳答案

我不认为它与引导项目有明确的联系,目前 AngularJS 根本不允许异步加载模块,请检查此线程:How is modularity mitigated in AngularJS?

基本上所有 AngularJS 模块都必须在应用程序引导之前加载。因此,在启动 AngularJS 应用程序之前,您需要确保您依赖的所有模块都已完全加载。

很乐意提供更多帮助,但仅根据错误代码,几乎不可能说更多。

关于angularjs - 异步加载 AngularUI Bootstrap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15470651/

相关文章:

css - 移动设备中的可折叠 DIV

javascript - AngularJS 中的重定向无法部分工作

javascript - Angular $modal 有什么问题?

javascript - AngularJS 中的子菜单(展开/折叠树)

javascript - 导航后设置 angular-ui-tinymce 编辑器内容

angularjs - Angular ui 路由器 : ui-views vs directives?

css - 使用 angular-1.3 获取 angular-bootstrap 模态

javascript - 为什么 Firefox、Safari 和 Chrome 之间的滚动不一致?

javascript - phonegap 3.5 播放本地音频文件

angularjs - 无法从状态 '___' 解析 'home' - angular ui-router 问题