如果使用ui.router您可以用圖片做這樣的和更新的範圍。
這裏是一個代碼示例
'use strict';
angular
.module('myApp', [
'ui.router'
])
.config(function ($locationProvider, $stateProvider, $urlRouterProvider, $compileProvider) {
// define states
$stateProvider
.state('home', {
url: '/',
templateUrl: 'views/home.html'
})
.state('about', {
url: '/about',
templateUrl: 'views/about.html'
});
// define alternative route
$urlRouterProvider.otherwise('/');
})
.run(function ($rootScope) {
// image changes with route change
if(toState.url === '/') {
$rootScope.headerImg = 'images/headers/home.jpg';
} else if (toState.url === '/about') {
$rootScope.headerImg = 'images/headers/about.jpg';
} else {
$rootScope.headerImg = 'images/headers/error.jpg';
}
});
});
感謝您的回答。我認爲存在誤解。我不想改變看法。我可以將html代碼添加到URL更改的現有視圖嗎? – Buga1234
當然。您可以使用相同的視圖多個路線...然後,您可以檢查路線是否正確,並設置正確的圖像..這是發生在我的代碼中,在'//圖像'後面的行中。 圖像隨着不同的路線。如果您對多個路線使用相同的視圖,則該視圖不會顯示。 – herrh
將在路由更改時重新加載視圖? – Buga1234