WearyMonkey / ngtemplate-loader

Include AngularJS templates in the Webpack bundle and preload the template cache.
MIT License
238 stars 78 forks source link

throws error when using inside ES6 Modules #33

Closed paynoattn closed 8 years ago

paynoattn commented 8 years ago

This is a great loader, I have a strange issue though. I got this to work fine, until I tried moving it to a ES6 modules.

It throws an error that it doesn't know what 'angular.module' is. Offending error is: 'Uncaught TypeError: Cannot read property 'module' of undefined' from my bundle: 'window.angular.module('ng').run(['$templateCache', function(c) { c.put(path, html) }])'

app.js

require('angular');
require('angular-ui-router');
var app = angular.module('app', ['ui.router']);
app.config(AppConfig);
import AppConfig from './app-config.js';

app-config.js

AppConfig.$inject = ['$stateProvider', '$urlRouterProvider'];

export default function AppConfig($stateProvider, $urlRouterProvider) {
    //define an app-wide abstract state for ui-router
    $stateProvider.state('app', {
        url: '/',
        //controller: 'AppController as AppController',
        templateUrl: appPartial
    });
    $urlRouterProvider.otherwise('/');
}

import appPartial from 'ngtemplate!html!./app.partial.html';
paynoattn commented 8 years ago

Whoops, I forgot to import 'angular' in my app-config.js. I didn't think I needed it because I wasn't using any props of the angular object, but apparently you do.

fix is for app-config.js:

import 'angular';

AppConfig.$inject = ['$stateProvider', '$urlRouterProvider'];

export default function AppConfig($stateProvider, $urlRouterProvider) {
    //define an app-wide abstract state for ui-router
    $stateProvider.state('app', {
        url: '/',
        //controller: 'AppController as AppController',
        templateUrl: appPartial
    });
    $urlRouterProvider.otherwise('/');
}

import appPartial from 'ngtemplate!html!./app.partial.html';