【发布时间】:2016-01-14 07:50:15
【问题描述】:
嗨,我正在使用 Angular 和 Require。 Require 正在管理我的依赖序列。使用延迟加载加载控制器,我能够加载我的第一个家庭控制器,但是当我想导航到仪表板控制器时,我收到错误仪表板控制器未找到。这是代码
main.html
<body ng-app >
<div id="ViewPort" ng-view> </div>
</body>
shell.js
define(['mngRoute', 'mngSanitize'], function (ngRoute, ngSantize) {
var CMSapp = angular.module('CMS', ['ngRoute', 'ngSanitize']);
CMSapp.config(function ($routeProvider, $httpProvider) {
console.log($httpProvider);
$routeProvider
// route for the home page
.when('/', { templateUrl: 'App/Partials/home.html', resolve: loader(['Home']) })
.when('/Dashboard', { templateUrl: 'App/Partials/Dashboard.html', resolve: loader(['Dashboard']) })
});
CMSapp.run(function () {
console.log('shell loaded');
console.log(CMSapp);
});
return CMSapp;
Home Controller.js
define(function () {
angular.module('CMS').controller('HomeController', ["$scope", "$http", 'MainService', function ($scope, $http, mainService) {
$scope.message = 'Homess';
$scope.InitializeController = function ()
{
console.log(mainService);
mainService.initializeApplication($scope.initializeApplicationComplete, $scope.initializeApplicationError);
}
$scope.initializeApplicationComplete = function (response) {
console.log('in default initializeApplicationComplete');
}
$scope.initializeApplicationError = function (response) {
console.log('in default intialize error');
}
}]);
});
主页.html
<div data-ng-controller="HomeController" ng-init="InitializeController()">
<span>{{message}}</span>
Dashboardcontroller 和dashboard.html 相同,只是名称不同
define(function () {
angular.module('CMS').controller('DashboardController', ["$scope", "$http", 'MainService', function ($scope, $http, mainService) {
$scope.message = 'Dashboard';
$scope.InitializeController = function ()
{
console.log(mainService);
}
}]);
});
仪表板html
<div data-ng-controller="DashboardController" ng-init="InitializeController()">
<span>{{message}}</span>
我能够加载主页并能够与模型绑定,但是当我在 url 中输入 localhost:8999/Main.html#/Dashboard 时,这不在仪表板中,它给出了错误。
查看 chrome 控制台Full console
Angular、Route、Dasboardcontroller.js homecontroller.js shell.js 加载成功,控制台没有错误,只有错误是 Dasboard 控制器不是函数,已定义
【问题讨论】:
-
在页面上初始化angular应该是
ng-app="CMS"
标签: angularjs