以下是一個簡單的準系統應用程序,它可以完成您想要的任務。請記住,我使用routeProvider模塊而不是stateProvider,但是同樣的原則適用。
我已經爲你製作了live Plunker演示。有關如何使用$ timeout的更多信息,請單擊here。
相關部分是:
app.controller('MainCtrl', function($scope, $timeout, $location) {
// Main controller
$scope.title = "First Page";
var goToSecondPage = function() {
$location.path('/second');
};
$timeout(goToSecondPage, 5000);
});
app.js
var app = angular.module('plunker', ['ngRoute']);
app.controller('MainCtrl', function($scope, $timeout, $location) {
// Main controller
$scope.title = "First Page";
var goToSecondPage = function() {
$location.path('/second');
};
$timeout(goToSecondPage, 5000);
});
app.controller('SecondCtrl', function($scope, $timeout, $location) {
// Second controller
$scope.title = "Second Page";
var goToMainPage = function() {
$location.path('/');
};
$timeout(goToMainPage, 5000);
});
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: "main.tpl.html",
controller: 'MainCtrl'
})
.when('/second', {
templateUrl: "second.tpl.html",
controller: 'SecondCtrl'
})
.otherwise({ redirectTo: '/' });
}]);
的index.html
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="[email protected]" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.9/angular-route.js"></script>
<script src="app.js"></script>
</head>
<body>
<h1>My Angular App</h1>
<ng-view></ng-view>
</body>
</html>
main.tpl。HTML
<h2>{{title}}</h2>
<p>You will be redirected to the second page in 5 seconds...</p>
second.tpl.html
<h2>{{title}}</h2>
<p>This is the second page.</p>
<p>You will be redirected to the main page in 5 seconds...</p>
是我已經設置了路線和控制器......我想你的代碼集成到我的代碼,但它似乎沒有做什麼?有沒有另一種方法來做到這一點?非常感謝您幫助btw – Freedom
您可以發佈控制器的代碼片段 – Jake