Angular 2 routerLink可选路由部分

时间:2017-12-14 22:49:03

标签: angular2-routing

我想用相对的routerlink实现类似的东西:

[routerLink]=[routePart, 'Create']

其中routePart是一个组件属性,可以为空,意味着它应该被忽略,或者有一些值

因此,如果当前路线是'/ Products',那么

routePart =''我们想得到'/ Products / Create'

routePart ='xyz'我们想得到'/ Product / xyz / Create'

但是我在第一种情况下发现它变成了绝对路径并解析为'/ Create'

我可以使用绑定中的表达式来使用它,但这看起来有点难看

[routerLink]='routePart == '' ? 'Create' : 'xyz/Create''

是否有一些干净的方法让这个工作?

1 个答案:

答案 0 :(得分:0)

试试这个。

<html>
    <head>
        <meta charset="UTF-8">
        <title>Innisnotfree</title>
    </head>
    <body>
        <?php
            session_start();
           
            include'header.php';
            include'nav.php';
            include "connection.php";
            
            $sql = "SELECT * FROM products";
            $res = mysqli_query($connection, $sql);
        ?>
        
        <div class="container">
            
            <div class="row">
               
                <?php while($r = mysqli_fetch_assoc($res)){ ?>
                <div class="col-sm-6 col-md-3">
                    <div class="thumbnail">
                        <img src="<?php echo $r['image'];?>" alt="<?php echo $r['title']?>">
                        <div class="caption">
                            <form name="form1" id="form1" method="POST" action="addtocart.php?id=<?php echo $r['id']?>">
                                <h3><?php echo $r['title']?></h3>
                                <p> <?php echo $r['description']?></p>
                                <p><?php echo $r['price']?></p>
                                <p><input type="number" name="quantity" id="quantity" class="form-control"/></p>
                                <p><input type="submit" name="add_to_cart" id="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Add to Cart"/></p>
                            </form>
                        </div>
                    </div>
                </div>
                <?php } ?>
            </div>
        </div>
        
        <?php include('footer.php')?>
 
    </body>
</html>
相关问题