[英]How to make a 'top-level'-controller in angular-js?
AngularJS newbie here.... I have a basic framework of an app located here: AngularJS新手在这里...。我在这里有一个应用程序的基本框架:
http://plnkr.co/edit/HlQvMKF735YrBL2tOYGf?p=preview http://plnkr.co/edit/HlQvMKF735YrBL2tOYGf?p=preview
The app contains an app.js and a couple of sub-models/controllers. 该应用程序包含一个app.js和几个子模型/控制器。
app.js: app.js:
var app = angular.module('MyApp', ["ngRoute","page1","page2"]);
app.config(['$routeProvider', function ($routeProvider) {
//$routeProvider.otherwise({redirectTo: '/page1/'}); // this works just fine if uncommented
}]);
app.controller('MainCtrl', function($scope) { // none of this works
$scope.pageTitle = "Page Title"; //
$scope.myName = 'Scott';
$scope.hideSessionInfo = true;
$scope.toggleSessionData = function() {
console.log('toggleSessionData');
$scope.hideSessionInfo = !$scope.hideSessionInfo;
};
});
I want there to be a top-level controller to handle states of items outside of the ng-view. 我希望有一个顶级控制器来处理ng-view之外的项目状态。 (ie showing/hiding some server session info)
(即显示/隐藏一些服务器会话信息)
I can't get this outer-level controller to work. 我无法使该外部控制器工作。 The controllers on the routes work just fine.
路线上的控制器工作正常。
I know I'm doing something fundamentally wrong, but I can't figure it out. 我知道我做的事情根本上是错误的,但是我无法弄清楚。
Fixed by adding the ng-controller onto the body and giving it my global controller name. 通过将ng-controller添加到主体并为其指定我的全局控制器名称来解决。
Thanks, Scott 谢谢,斯科特
<div ng-controller ="MainCtrl">
<div ng-view></div>
</div>
Should usually work. 通常应该可以工作。 You need to attach your controllers via a directive (here ng-controller and ng-view, which switches the controller depending on the route).
您需要通过指令附加控制器(此处为ng-controller和ng-view,它们根据路由来切换控制器)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.