使用webpack

时间:2019-02-21 20:33:57

标签: angularjs webpack

我的webpack.config

var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {

entry: "./main.js", //relative to root of the application
output: {
    path: __dirname,
    filename: "app.bundle.js" //relative to root of the application
},
watchOptions: {
    aggregateTimeout: 300,
    poll: 1000
},
plugins: [
    new HtmlWebpackPlugin({
        hash: true,
        title: 'My Awesome application',
        myPageHeader: 'interviewee',
        template: './_index.html',
        filename: 'index.html' //relative to root of the application
    })
]

}

main.js

    let jquery = require("./Scripts/jquery-1.9.0.js");
    let angular = require("./Scripts/angular.js");
    let ngRoute = require("./Scripts/angular-route.js");
    let bootstrap = require("./Scripts/bootstrap.js");
    let appController = require("./app.controller.js");
    let addController = require("./add.controller.js");
    let service = require("./service.js");
    let messages = require("./Scripts/angular-messages.js")

app.controller

    var MyApp = angular.module("MyApp", [
        'ngRoute',
        'ngMessages',
        'IntervieweeService'
        ]
    );
    MyApp.config(['$routeProvider',
        function ($routeProvider) {
            $routeProvider.
                when('/Add', {
                    templateUrl: 'Views/add.html',
                    controller: 'AddController'
                }).

                otherwise({
                    redirectTo: '/Home'
                });
        }]
    );

add.controller

    MyApp.controller("AddController", function ($scope, EmpApi) { ....... }

运行应用程序时,未定义“ MyApp”。我究竟做错了什么?我是webpack和angularjs的新手。您能告诉我如何解决此问题吗?谢谢

2 个答案:

答案 0 :(得分:0)

以精简版本可以理解的方式更改您的控制器依赖性。即

MyApp.controller("AddController", function ($scope, EmpApi) { ....... }

MyApp.controller("AddController", addController);

addController.$inject = ['$scope', 'EmpApi'];

function addController($scope, EmpApi){...};

答案 1 :(得分:0)

控制器文件中未定义“ MyApp”,因此您必须将控制器添加到“ MyApp”模块中

更改

MyApp.controller("AddController", function ($scope, EmpApi) { ....... }

收件人

angular.module('MyApp').controller("AddController", function ($scope, EmpApi) { ....... }