带有routeProvider的href加载新页面,而不是绑定视图 [英] href with routeProvider loads a new page instead of binding the view
问题描述
我被routeProvider困住了.我的链接会继续打开新页面,而不是在视图中加载模板.
I'm stuck with routeProvider. my links keep opening a new page instead of loading the template in the view.
我的代码如下
raceApp.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'raceApp/createChallenge_view.php',
controller: 'createChallenge'
})
.when('/createchallenge', {
templateUrl: 'raceApp/createChallenge_view.php',
controller: 'createChallenge'
})
.when('/findchallenge', {
templateUrl: 'raceApp/findChallenge_view.php',
//controller: 'findChallenge'
})
.otherwise({
redirectTo: '/'
});
}]);
我的HTML看起来像
<base href="/runkinlocal/wp-content/themes/wordpress-bootstrap-master/" />
<a class="menu-item select-runner0" href="#/createChallenge">
<a class="menu-item select-runner0" href="#/findchallenge">
页面加载时,将加载初始模板,但是当我单击链接之一时,将加载新页面,而不是在视图中加载模板 (该页面看起来像 http://test. com:8888/runkinlocal/wp-content/themes/wordpress-bootstrap-master/#/createChallenge )
When the page loads, the initial template is loaded, but then when I click on one of the links, a new page loads instead of loading the template in the view ( the page looks like http://test.com:8888/runkinlocal/wp-content/themes/wordpress-bootstrap-master/#/createChallenge)
我正在使用Wordpress.
I am working with Wordpress.
期待您的帮助!
推荐答案
工作示例
plnkr
<a href="createChallenge">Create</a> |
<a href="findChallenge">Find</a>
2.否则不需要redirectTo
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'home.html'
})
.when('/createChallenge', {
templateUrl: 'create.php',
controller: 'CreateChallengeController'
})
.when('/findChallenge', {
templateUrl: 'find.php',
controller: 'FindChallengeController'
})
.otherwise("/");
// configure html5 to get links working on jsfiddle
$locationProvider.html5Mode(true);
});
3.最后一个.when()之后的逗号
您有:
3. Extra comma after last .when()
You have:
.when('/findchallenge', {
templateUrl: 'raceApp/findChallenge_view.php',
//controller: 'findChallenge'
})
控制器已被注释掉,但是仍然有一个逗号.
The controller is commented out but there is still a comma.
(请参见 plnkr 中的script.js)
(see script.js in the plnkr)
// configure html5 to get links working on jsfiddle
$locationProvider.html5Mode(true);
5.您可以像这样添加基本href
<script type="text/javascript">
angular.element(document.getElementsByTagName('head')).append(angular.element('<base href="' + window.location.pathname + '" />'));
</script>
我建议检查一下:
让我知道是否可行!
这篇关于带有routeProvider的href加载新页面,而不是绑定视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!