我有有一个介绍页面和“工作”页面的角度JS应用.. 我的HTML的一个简化版本是这样的:如何获取多个Angular JS路由以共享单个控制器实例?
(function() {
'use strict';
var MyController = function (_, $rootScope, $scope, $location, $anchorScroll, $timeout, gettextCatalog, service) {
var self = this;
console.log("test");
//More code which invokes http requests
};
angular.module('my.controller', [ 'gettext', 'lodash', 'ngRoute', 'ngSanitize', 'ngAnimate', 'my.service' ]).config(
function ($routeProvider) {
$routeProvider.when('/', {
'templateUrl': 'modules/home.html',
'reloadOnSearch': false
}).when('/chatting', {
'templateUrl': 'modules/working.html',
'reloadOnSearch': false
});
}).controller('MyController', MyController);
}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<html id="ng-app" ng-app="app" ng-controller="MyController as myCtrl">
<head>
<link rel="stylesheet" href="styles/main.css">
<title>What's in Theaters</title>
</head>
<body id="app-body" class="dialog-body">
<div style="height: 100%; width: 100%" ng-view></div>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-gettext/dist/angular-gettext.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="bower_components/angular-animate/angular-animate.js"></script>
<script src="app.js"></script>
<script src="modules/my-controller.js"></script>
</body>
</html>
我的问题是,当我从一个路由移动到另一个路由时,即从“/”到“/#working”,MyController重新初始化,并且已经完成的http请求现在被丢弃。如何在两个节点之间共享同一个控制器实例路线?