AngularJS路由不起作用:链接点击时没有事件,没有$routeParams

AngularJS routing is not working: no events on link clicks, no $routeParams

本文关键字:事件 没有 routeParams 不起作用 路由 链接 AngularJS      更新时间:2023-09-26

不知何故,我的应用程序在开发过程中停止工作,我真的无法理解它出了什么问题。

我已经删除了所有内容,剩下的唯一代码是:

function handlerControl($scope, $routeParams, $location){
    $scope.route = $routeParams;
}
var app = angular.module('Hello', []).config(
    function($routeProvider){
        $routeProvider.when('/:a/:b', {controller: handlerControl});
    }
);

而 html 是

<body ng-app="Hello">
<div ng-controller="handlerControl">
    {{route}}
</div>
</body>

省略头部,包括所有内容。

当我去

http://helloday/#/a/b/

我在期望得到 {a: 'a', b: 'b'} 时得到一个空哈希

我做错了什么?

位修改(使其工作) jsFiddle: http://jsfiddle.net/wWDj2/http://jsfiddle.net/wWDj2/

路由要求您使用 ngView,并且指定templatetemplateUrl

应用代码。

var app = angular.module('myApp', []);
app.config(function($routeProvider) {
   $routeProvider.when('/foo/:id', {
        controller: 'FooCtrl',
        template: '<h1>Foo {{id}}</h1>'
   })
   .when('/bar/:test', {
        controller: 'BarCtrl',
        templateUrl: 'bartemplate.html'
   })
   .otherwise({ 
        controller: 'DefaultCtrl',
        template: '<h1>This is the default</h1>'
   });
});
app.controller('FooCtrl', function($scope, $routeParams) {
   $scope.id = $routeParams.id;
});
app.controller('BarCtrl', function($scope, $routeParams) {
   $scope.test = $routeParams.test;
});
app.controller('DefaultCtrl', function($scope){});

主页的标记:

<div ng-app="myApp">
   <a href="#/foo/123">Foo 123</a>
   <a href="#/bar/blah">Bar Blah</a>
   <a href="#">Default route</a>
   <hr/>
   <div ng-view>
       <!-- your processed view will show up here -->
   </div>
</div>