为什么foodModule没有定义错误?Angular js
why foodModule not defined error? Angular js
嗨,我一直在把它从路由文件中分离出来。但它给出了未定义的模块错误。有人能猜出问题吗?
main.js
var foodModule = angular.module('scratchpad',['ui.router','ngResource']);
route.js
console.log('route.js included successfully');
foodModule.config(function($stateProvider,$httpProvider) {
$stateProvider.state('home',{
url:'/home',
templateUrl:'templates/food.html',
controller:'addUserController'
})
.state('scratchpad',{
url:'/scratchpad',
templateUrl:'templates/scratchpad.html',
controller:'scratchListController'
})
.state('addNewScratch',{
url:'/addNewScratch',
templateUrl:'templates/addNewScratch.html',
controller:'addScratchController'
})
.state('scratchpad.viewScratch',{
url:'/scratchpad/:id/view',
templateUrl:'templates/viewScratch.html',
controller:'viewScratchController'
})
}).run(function($state){
$state.go('home');
});
错误:
Uncaught ReferenceError: foodModule is not defined
不要缓存你的angular模块,你正在将事情泄露到全局范围中。相反:
对于main.js:
//Just declare the module
angular.module('scratchpad',['ui.router','ngResource']);
对于路由js。。。这样做吗:
angular.module('scratchpad')
.config(function($stateProvider,$httpProvider) {
$stateProvider.state('home',{
url:'/home',
templateUrl:'templates/food.html',
controller:'addUserController'
})
.state('scratchpad',{
url:'/scratchpad',
templateUrl:'templates/scratchpad.html',
controller:'scratchListController'
})
.state('addNewScratch',{
url:'/addNewScratch',
templateUrl:'templates/addNewScratch.html',
controller:'addScratchController'
})
.state('scratchpad.viewScratch',{
url:'/scratchpad/:id/view',
templateUrl:'templates/viewScratch.html',
controller:'viewScratchController'
})
}).run(function($state){
$state.go('home');
});
更好的是,把它们包在IIFE里。
请参阅John Papa的Angular风格指南以获得更好的解释。。。
我建议用大口/咕噜声把它们捆在一起,这样它们的顺序就正确了,可以把它们捆起来吃。除非使用require/system-js之类的模块加载程序,否则不能保证首先执行main js。以前在IE中被类似的东西咬过。也许这篇文章可以揭示这个问题,但没有什么是好的构建系统无法解决的,所以是的。。。
您可能会下这样的订单:-
<script src="route.js"></script>
<script src="main.js"></script>
应该是这样的:-
<script src="main.js"></script>
<script src="route.js"></script>
相关文章:
- Angular JS IE9 Hashbang url rewriting
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- angular.js没有'无法在PhoneGap中处理视图标记
- Angular js-返回一个包含类似
- 如何使用 Angular JS 将数据保存在数据库中
- 将Angular js与taglib结合使用
- Angular JS Filter-通过3个复选框进行筛选
- vaadin:使用自定义布局集成angular js
- angular js密码强度显示问题
- 使用angular.js问题的JavaSpringREST服务
- 将复杂对象从angular js传递到web api,它总是返回404
- entre上的angular js阻止提交
- 根据Angular.JS上一次的内容禁用选择
- 如何在angular js中使用指令.不适用于我的情况,为什么
- 预期响应包含一个对象,但在angular js中得到一个数组错误
- angular.js:13424错误:[ng:areq]参数'fn'不是函数,getObject
- 使用Angular js-量角器e2e测试上传文件
- Angular.js延迟控制器初始化
- 如何在angular.js中动态应用自定义过滤器
- 谷歌地图/Angular JS:地图更新并不总是反映数据,并且可以't删除标记