【问题标题】:Angular + angularAMD, load a controller before bootstrapping the appAngular + angularAMD,在引导应用程序之前加载控制器
【发布时间】:2013-12-20 16:33:38
【问题描述】:

首先,感谢这个伟大的插件! (致 angularAMD 的作者)

我有一些麻烦。我已经用 ngAMD 加载了所有模块,但有两个在我的 index.html 中,因为它们是模板,我以这种方式包括在内:

 <div ng-include="'views/header.html'"></div>

header.html 使用 HeaderCtrl 但我不知道在 angularAMD.bootstrap 之前如何加载...

更多代码:

header.coffee 需要应用程序

define ['app', 'bootstrap'], (app, bs) ->
  'use strict'

  app.controller 'HeaderCtrl', ($scope, $rootScope) ->
    $scope.searchText = "";

    $scope.updateSearch = -> 
      $rootScope.searchText = $scope.searchText;

app.coffee

define ['angular', 'angularAMD'], (angular, angularAMD) ->
  'use strict'

  app = angular.module 'testsApp', [
    'ngRoute'
    'localization'
    'restangular'
  ]

  angularAMD.bootstrap app
  app

启动应用程序后,ng 尝试解决 ng-include 但 HeaderCtrl 未加载!这仅在页面上使用 CTRL+F5 时才会发生,这是加载时间的问题。我不知道如何解决这个问题。有什么提示吗?

【问题讨论】:

  • 您还有问题吗?如果你这样做了,设置一个简单的 plunker,我会看看我能做些什么来提供帮助。
  • @marcoseu:谢谢。我已经解决了。我会尽快将解决方案上线。

标签: angularjs amd


【解决方案1】:

问题是angularAMD.bootstrap app 位置。我在加载每个依赖项之前进行引导。

我在 dep 加载后移动了 angularAMD.bootsrapp 调用,现在一切正常:

bootstrap.coffee(应用程序引导程序)

define [
  'app'
  'jquery'
  '_'
  'angularAMD'
  'ctrl/header'
  'ctrl/menu']
, (app, $, _, angularAMD) ->

  # bootstrapping all dependencies
  $.get "modules/modules.json", (deps) ->
    # converts all array items from DEP to module!DEP
    deps = _.map deps, (dep) ->
      "module!#{dep}"

    console.log JSON.stringify deps
    require deps, ->
      console.log "Dependencies loaded!"
      # I'll bootstrap angular only after each dependencies are loaded, this prevents many 'random' issues.
      angularAMD.bootstrap app

我希望这可以帮助某人:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 2018-01-16
    • 2020-05-31
    • 2014-10-03
    相关资源
    最近更新 更多