0
我敢肯定,這是一個非常愚蠢的問題,但我是新來的angularjs,並試圖找到解決方案,但似乎沒有任何工作。這可能是我解釋路由器錯了。沒有加載angularjs控制器(不使用ng控制器)
我的控制器只在我將ng-controller添加到html文件時加載,如果我不是這樣,但我認爲路由器會照顧它。我在這個例子中使用了requirejs並縮小了我的腳本。
的index.html
<!doctype html>
<html ng-app>
<head>
<!-- load our application -->
<script src="assets/vendor/requirejs/require.js" data-main="assets/js/config"></script>
</head>
<body id="splash">
hehe my page
</body>
</html>
config.js
requirejs.config({
baseUrl: 'assets/js',
paths: {
angular: '..\\vendor\\angularjs\\angular',
bootstrap: '..\\vendor\\bootstrap',
jquery: '..\\vendor\\jquery\\jquery'
},
shim: {
bootstrap: [
'jquery'
],
angular: {
deps: ['jquery'],
exports: 'angular'
}
}
});
require(['app/app']);
app.js
define(['angular'], function (angular) {
angular.module('fettnerd.controllers', []);
angular.module('fettnerd', ['fettnerd.controllers']);
require(['app/routing'], function() {
angular.bootstrap(document, ['fettnerd']);
});
});
router.js
define(['angular', 'app/controllers/IndexCtrl'], function(angular) {
angular.module('fettnerd')
.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true); // we want history api
$routeProvider.when("/home", {
controller: "IndexCtrl"
});
$routeProvider.otherwise({redirectTo: '/home'});
});
});
個IndexCtrl.js
define(['angular', 'jquery'], function(angular, $) {
angular.module('fettnerd.controllers')
.controller('IndexCtrl', function($scope) {
console.log('test');
$scope.login = function(e) {
alert('Soon. How soon? Very soon.');
}
});
});
哈哈我知道這真的很愚蠢。非常感謝! – wardpeet