Angular routeParams not working URL is changing

无人久伴 提交于 2020-01-03 02:42:07

问题


I have a button in HTML in which it has the correct data rendered.

THIS part seems to work FINE

module.js

.when('/tips/:group?', {
    templateUrl: 'partials/tips.html',

controller.js

.controller('TipsCtrl',
  // code
  $scope.groupid = $routeParams.group;

HTML:

<a href="#/tips/comments/{{ tip.id }}?groupid={{ groupid }}" c class="btn-yellow">Add / View Comments</a>

URL:

<a href="#/tips/comments/10274?groupid=5" class="btn-yellow">Add / View Comments</a>

Part that is NOT working

module.js

.when('/tips/comments/:group?:groupid', {
    templateUrl: 'partials/comments.html'

controller.js

.controller('TipCommentsCtrl',
$scope.groupid = $routeParams.group;
$scope.detailFrame = $sce.trustAsResourceUrl("http://localhost:17308/Home/AddComment/" + group);

URL in Browser after clicking button

http://localhost:1337/doc-home/#/tips/comments/1?status=new

Why is this happening?

My URL for my

<iframe ng-src="http://localhost:17308/Home/AddComment/1" align="middle" width="1000" height="800" frameborder="0" src="http://localhost:17308/Home/AddComment/1">
        &lt;p&gt;Your browser does not support iframes.&lt;/p&gt;
    </iframe>

IF I CHANGE THE CODE IN module.js

Browser URL is better , not completely right.

-->  http://localhost:1337/doc-home/#/tips/comments/10274?status=new 

This happens if I have to .when in module.js to be

.when('/tips/comments/:group?', {
    templateUrl: 'partials/comments.html'

But the URL is not going to have the additional parameter that I NEED

 <iframe ng-src="http://localhost:17308/Home/AddComment/10274" align="middle" width="1000" height="800" frameborder="0" src="http://localhost:17308/Home/AddComment/10274">
        &lt;p&gt;Your browser does not support iframes.&lt;/p&gt;
    </iframe>

I want to have:

http://localhost:17308/Home/AddComment/10274?groupid=5

回答1:


Ok This routing should work for you then:

 module.js -->   Notice the groupid (which you could say anything like 'id'  
 .when('/tips/comments/:group?:groupid?', {
    templateUrl: 'partials/comments.html'

 Next your controller code

 controller.js -->   Notice that I take a scope param and at to your url

  $scope.groupid = $routeParams.groupid;
  $scope.detailFrame = $sce.trustAsResourceUrl("http://localhost:17308/Home/AddComment/" + group + "?groupid=" + $scope.groupid);

This should work for you. Now your IFrame will have that correct url in the variable that gets rendered.



来源:https://stackoverflow.com/questions/32276886/angular-routeparams-not-working-url-is-changing

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