$ route不适用于搜索网址

时间:2012-10-25 19:55:01

标签: javascript angularjs

我的路线示例:

angular.module('test', [], function ($routeProvider) {
$routeProvider.when('/', {
    templateUrl: 'pageOne.html',
    controller: ControllerOne
}).when('/?:foo, { templateUrl: '
pageTwo.html ', controller: ControllerTwo });`

但是,如果我加载http://example.com/?someFoo之类的页面,我的路由不会加载ControllerTwo,它会加载ControllerOne。我做错了什么?感谢。

3 个答案:

答案 0 :(得分:0)

我认为格式不正确。

angular.module('test', [], function ($routeProvider) {
$routeProvider.when('/', {
    templateUrl: 'pageOne.html',
    controller: 'ControllerOne'
}).when('/foo/:foo', { templateUrl: '
pageTwo.html ', controller: 'ControllerTwo' });

并加载http://example.com/#/foo/someFoo

之类的页面

我建议你有

.otherwise({ redirectTo: '/nosuchpage.html'})

然后你会知道你的链接是否错误。

答案 1 :(得分:0)

您需要将控制器名称作为字符串传递

这有效

var myApp = angular.module('myApp',[],function($routeProvider) {        

    $routeProvider
        .when('/home',{templateUrl:'home.html'})
        .when('/page1',{templateUrl:'page1.html', controller:'ctrl1'})
        .when('/page2',{templateUrl:'page2.html', controller:'ctrl2'})
        .otherwise({redirectTo:'/home'});

});

function ctrl1($scope){
    $scope.something = 'Hello from page 1';
}
function ctrl2($scope){
    $scope.something = 'Hello from page 2';
}

jsfiddle:http://jsfiddle.net/jaimem/T2TWB/1/

答案 2 :(得分:0)