Angular routeParams not working URL is changing

2019-06-06 13:15发布

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条回答
欢心
2楼-- · 2019-06-06 13:44

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.

查看更多
登录 后发表回答