Angular+Requirejs-每个数据重复两次
Angular + Requirejs - each data is repeated twice?
我在Requirejs下运行Angular,但我多次出现控制器类中的数据重复两次的情况。除非我从div
中删除ng-app="myApp"
。为什么?
welcomeController.js,
define(['app'], function (app) {
app.controller('welcomeController', ['$scope', function($scope) {
//your minsafe controller
$scope.message = "Message from WelcomeController";
console.log($scope.message); // it is repeated twice here.
}]);
});
HTML,
<head>
<link rel="stylesheet" href="style.css">
<script data-main="scripts/main.js" src="scripts/vendors/requirejs/require.js"></script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="welcomeController">
{{message}}
</div>
</div>
</body>
main.js,
require.config({
//baseUrl: "",
// alias libraries paths. Must set 'angular'
paths: {
'domReady': 'vendors/requirejs-domready/domReady',
'angular': 'vendors/angular/angular',
'angular-route': 'vendors/angular-route/angular-route',
'jquery': 'vendors/jquery/dist/jquery'
},
// Add angular modules that does not support AMD out of the box, put it in a shim
shim: {
'angular': {
exports: 'angular'
},
'angular-route': {
exports: 'angular'
}
}
});
define([
'controllers/welcomeController',
'bootstrap'
]);
bootstrap.js,
define([
'require',
'angular',
'app'
], function (require, ng) {
'use strict';
require(['domReady!'], function (document) {
ng.bootstrap(document, ['myApp']);
});
});
app.js,
define([
'angular'
], function (ng) {
'use strict';
//For single module retrun.
return ng.module('myApp', []);
});
但在没有Requirejs的情况下,它在Angular上运行良好。我不需要从div
中移除ng-app="myApp"
。为什么?
<head>
<link rel="stylesheet" href="style.css">
<script src="scripts/vendors/angular/angular.js"></script>
<script>
var myApp = angular.module('myApp',[]);
myApp.controller('welcomeController', ['$scope', function($scope) {
$scope.message = "Message from WelcomeController";
console.log($scope.message); // It occurs only once.
}]);
</script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="welcomeController">
{{message}}
</div>
</div>
</body>
我在使用Angular的Requirejs下做错了什么?
您正在引导Angular两次:一次使用ng-app
指令,一次使用bootstrap.js中的ng.bootstrap(...)
代码!转到自动(ng-app
)或手动(angular.bootstrap
)引导。
此外,正如Josep在他(已删除)的回答中提到的那样,最好将angular-route
填充为:
shim: {
'angular-route': {
deps: ['angular']
}
}
这不是问题的原因,但将防止未来潜在的问题(即在Angular之前加载路由)。
angular require lazy可能包含一些有用的想法,用于angular RequireJS与lazy加载的集成
相关文章:
- Meteor Router数据函数被调用两次
- 从MySQL数据库中获取输入数据需要两次页面刷新
- 为什么DTM数据元素被调用两次
- 模态中的数据关闭在单击时被调用两次
- jQuery 每个循环返回数据两次
- AngularJS自定义过滤器调用了两次,并在第二次调用时删除输入数据
- MVC3 AJAX将数据传递给控制器.它's已提交两次
- jQM选择菜单数据本地菜单=“;false”;跑两次
- 挖空模板:afterRender 触发两次,因为名称/数据是耦合的可观察量
- 在发送更新数据之前,表格需要提交两次
- 我的 JSON 数据使用 AJAX 加载了两次,它只应该在单击时加载一次
- 反应/回流数据流:DOM 中一个组件两次
- JS发送数据两次
- Angular+Requirejs-每个数据重复两次
- extjs4:如何两次提交相同的表单数据
- AngularJS.使用缓存时,防止加载JSON数据两次
- 如何将数据两次推入对象
- 在Popup上,通过ajax: php mysql可以看到两次数据获取
- 按钮由数据表生成,运行事件两次
- React -防止生命周期组件渲染相同的数据两次:WillReceiveProps &componentDidMount