paolo.basso99 paolo.basso99 -4 years ago 193
AngularJS Question

Laravel mix and AngulaJs

I'm trying to use Laravel Mix, which runs webpack, to compile an angular app into one file. I get the error:

Uncaught ReferenceError: app is not defined


my
webpack.mix.js
:

const { mix } = require('laravel-mix');

mix.js('resources/assets/js/dependencies.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');


my
bundle.js
:

//load angular
require('angular');

//Load Angular's plugins
require('angular-ui-router');

//Init Angular app
require('./app/app');

//Load angular controllers
require('./app/Controller/aboutController');
....

//Load angular directive
require('./app/Directive/directive');

//Load angular services
require('./app/Services/AccountService');
....


My
app/app.js
:

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

app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function ($stateProvider, $urlRouterProvider, $locationProvider) {

$urlRouterProvider.otherwise('/home');

$stateProvider
.state('home', {
url: '/home',
templateUrl: 'app/View/homeView.html',
controller: 'homeController'
})
...
}]);


app.run(['$state', '$rootScope', function ($state, $rootScope) {
//APP RUN
}]);


I get the
Uncaught ReferenceError: app is not defined
when I call
app.controller()
, for example
app/Controller/aboutController.js
:

app.controller("aboutController", ['$scope', '$rootScope', '$http', function ($scope, $rootScope, $http) {....}


Am i missing something? If i load all the file one by one using
html
it works fine.

Answer Source

If you are defining your app's controller in separate file , then you cannot use directly app as there is no reference . You first have to refer to the module .

angular.module('app')
  .controller('aboutController'....

Let me know if this work for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download