简体   繁体   English

AngularJS无法从控制器路由

[英]AngularJS cannot route from controller

I am using angularJS routing like this 我正在像这样使用angularJS路由

$routeProvider.
      when('/wall', {controller:DashboardCtrl, templateUrl:'/chutirghonta_repo/contentpanel/views/dashboard.html'}).
      when('/books', {controller:BooksCtrl, templateUrl:'/chutirghonta_repo/contentpanel/views/books.html'}).
      when('/tests', {controller:TestsCtrl, templateUrl:'/chutirghonta_repo/contentpanel/views/tests.html'}).
      when('/createbook', {controller:CreateBookCtrl, templateUrl:'/chutirghonta_repo/contentpanel/views/bookcreation.html'}).
      when('/createbookpage', {controller:CreateBookPageCtrl, templateUrl:'/chutirghonta_repo/contentpanel/views/bookpagecreation.html'}).
      otherwise({redirectTo:'/wall'});
      ;

Now, in one of my controllers, I have to redirect to a another route, so I am using 现在,在我的一个控制器中,我必须重定向到另一条路由,所以我正在使用

$scope.add = function(){
    window.location = "/testwebsite/contentpanel/#/createbook";
}

The above code does not work. 上面的代码不起作用。 It takes me back to the default route, which is 它带我回到默认路由,即

window.location = "/testwebsite/contentpanel/#/wall";

But, amazingly, the code below works and leads me to the desired page 但是,令人惊讶的是,下面的代码可以正常工作,并将我带到所需的页面

$scope.add = function(){
    window.open("/testwebsite/contentpanel/#/createbook");
}

In my view layer, I have a html page with this code snippet 在我的视图层中,我有一个带有此代码段的html页面

<a href="#" ng-click="add()" class="btn btn-primary">Add New Book</a>

I am out of ideas. 我没主意。 window.open works but window.location does not work. window.open有效,但window.location不起作用。

instead of 代替

window.location = "/testwebsite/contentpanel/#/createbook";

use 采用

$location.url('/createbook')

Solved it. 解决了。

I changed 我变了

<a href="#" ng-click="add()" class="btn btn-primary">Add New Book</a>

to

<button ng-click="add()" class="btn btn-primary">Add New Book</button>

Also, as Arnaud Gueras mentioned, 另外,正如Arnaud Gueras所述,

it is better to use 最好用

$location.url('/createbook');

instead of 代替

window.location = '/testwebsite/contentpanel/#/createbook';

because the $location.url specifies only the last part of the path, where as window.location specifies the whole path (which might change when my hosting changes) 因为$ location.url仅指定路径的最后一部分,而window.location指定整个路径(当我的主机更改时,它可能会更改)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM