2014-06-12 29 views
1

我有NG-網格,定義應用程序時,一個依賴性:是否有可能在運行時使用angularAMD延遲加載並將角度模塊注入應用程序?

var app = angular.module('myApp', ['ngGrid']); 

但並不是所有的視圖和控制器需要ngGrid,所以我想,如果它可能是可能加載並注入ngGrid到應用程序,而定義需要它的控制器?

我嘗試財產以後這樣的,但它沒有工作:

app.js:

var app = angular.module('app',[]); 

ProductListCtrl.js:

define(['app', 'ng-grid'], function (app) { 
    'use strict'; 

    app.register.controller('ProductListCtrl', ['$scope', 'ngGrid', function ($scope) { 
     name = $injector.get('ngGrid') 
     $scope.name = name 
    }]); 

}); 

有什麼建議?

回答

2

angularAMD提供了一個ngload RequireJS插件允許您加載現有的AngularJS模塊。添加ngloadmain.js然後執行:

define(['app', 'ngload!ng-grid'], function (app) { ... }

更多細節見documentation

+0

謝謝你的建議,但我仍然無法得到它的工作。我無法訪問Plunker,因此我創建了一個GitHub回購(https://github.com/chocstarfish/scalable-angular-app)。請您花一分鐘時間爲我檢查一下。謝謝! – NicolasZ

+0

順便說一句,它引發的錯誤是:未捕獲TypeError:無法讀取未定義的屬性'工廠'ng-grid-2.0.11.debug.js:209 錯誤:[$ injector:unpr]未知提供者:ngGridProvider < - ngGrid – NicolasZ

+0

@NicolasZ你需要創建一個只有一個頁面加載只是ng-grid的東西,因爲我無法調試你的整個應用程序。我看到的錯誤是「未知提供者:simpleClockModuleProvider < - simpleClockModule」。 – marcoseu

相關問題