Angular route when() without mapping to controller or template

孤者浪人 提交于 2019-12-12 15:44:39

问题


It's possible to use when() without mapping to any controller or template?

This is how I have configured my routes:

app.config(function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider
    .when('/presentations/:id', {});
});

I need to use routeParams in my controller:

app.controller('PresentationController', function($scope, $routeParams) {
    console.log($routeParams);
});

This doesn't work. I don't need ngView but I added it in case is needed. My controller is already loaded with the ng-controller directive.


回答1:


One has to continuously watch the $routeParams.id for changes in this case: http://plnkr.co/edit/x2cPxVWPqVePDule1aOk?p=preview

$scope.$watch(function () { return $routeParams.id; }, function (newVal) {
  if (typeof newVal === 'undefined') return;
  $scope.id = newVal;
})

Also, having an ng-view in the template is necessary.



来源:https://stackoverflow.com/questions/20181440/angular-route-when-without-mapping-to-controller-or-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!