Javascript 带有网页包的angular 1.6:控制器未注册
我正在使用angular为前端编写一个小应用程序,我的前端模块具有以下结构: 在我的app.js中,我定义了主要的角度模块:Javascript 带有网页包的angular 1.6:控制器未注册,javascript,angularjs,webpack,Javascript,Angularjs,Webpack,我正在使用angular为前端编写一个小应用程序,我的前端模块具有以下结构: 在我的app.js中,我定义了主要的角度模块: angular.module('weatherApp', []); 我的控制器(weather.js)正在注册WeatherCtrl: angular.module('weatherApp') .controller('WeatherCtrl', ['$scope', 'WeatherProvider', ($scope, WeatherProvider) =>
angular.module('weatherApp', []);
我的控制器(weather.js)正在注册WeatherCtrl:
angular.module('weatherApp')
.controller('WeatherCtrl', ['$scope', 'WeatherProvider', ($scope, WeatherProvider) => {
$scope.location = {
city: "",
country: ""
};
$scope.options = [
{ label: "Weather", value: "weather" },
{ label: "Forecast", value: "forecast" }
];
$scope.selected = $scope.options[0];
$scope.result = "Empty";
$scope.getWeather = function () {
WeatherProvider.getWeatherFunc($scope.selected.value, $scope.location.city, $scope.location.country)
.then((response) => {
$scope.result = response.data;
})
}
}]);
serevice(provider.js)正在注册WeatherProvider时:
angular.module('weatherApp')
.service('WeatherProvider', ['$http', function($http) {
this.getWeatherFunc = (weatherMode, city, country) => {
return $http({
method: 'GET',
url: "http://localhost:8080/" + weatherMode + "/" + city + "/" + country
})
}
}]);
我的网页包配置如下所示:
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
devServer: {
disableHostCheck: true
},
target: 'web',
context: path.join(__dirname, "src"),
devtool: 'source-map',
entry: {
app: "./app.js",
},
resolve: {
modules: [
path.resolve(__dirname),
"node_modules"
],
extensions: ['.js', '.html']
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
}
]
},
output: {
path: path.join(__dirname, 'src'),
filename: "bundle.js",
publicPath: "/"
},
plugins: [
new HtmlWebpackPlugin({
template: __dirname + "/src/index.tmpl.html"
}),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(process.env.NODE_ENV ||"dev"),
}
})
],};
以下是index.tmpl.html:
现在我正在运行npm run dev,给出了以下结果:
错误显示:error:$controller:ctrlreg
未注册具有此名称的控制器。
同时返回预期结果:
你知道为什么我的控制器和服务不在bundle.js中吗?(我相信这是失败的主要原因)试着改变顺序
<script src="app.js"></script>
<script src="service/provider.js"</script>
<script src="controller/weather.js"></script>
我使用了一个错误的webpack配置示例,以下行不适合我的情况:
target: 'web'
将其替换为:
target: 'node'
一切都按预期开始工作。创建一个plnkr。检查本教程您需要更多地处理您的app.js文件…我也尝试了此选项,但不幸的是,它不起作用