如何实现基于用户权限数组的动态菜单项列表?

时间:2018-07-04 09:12:00

标签: angular typescript angular6

我有一个应用程序,所有菜单项都来自下面的文件。我希望用户只能看到权限数组中的那些菜单?。下面是完整代码中的示例代码。

$db = mysqli_connect('localhost', 'cy57447_forms', '123456789', 'cy57447_forms');

if(isset($_POST['register'])) {
    $email = mysql_real_escape_string($_POST['email']);
    $tg_username = mysql_real_escape_string($_POST['tg_username']);
    $bitcointalk_username = mysql_real_escape_string($_POST['bitcointalk_username']);
    $bitcointalk_profile_link = mysql_real_escape_string($_POST['bitcointalk_profile_link']);
    $twitter_account_url = mysql_real_escape_string($_POST['twitter_account_url']);
    $number_of_followers = mysql_real_escape_string($_POST['number_of_followers']);
    $twitter_audit_link = mysql_real_escape_string($_POST['twitter_audit_link']);
    $ethereum_wallet = mysql_real_escape_string($_POST['ethereum_wallet']);
    $tokens = '';
}

    $sql = "INSERT INTO twitter_form (`tg_username`, `bitcointalk_username`, `bitcointalk_profile_link`, `twitter_account_url`, `number_of_followers`, `twitter_audit_link`, `ethereum_wallet`, `email`, `tokens`) VALUES ('$tg_username', '$bitcointalk_username', '$bitcointalk_profile_link', '$twitter_account_url', '$number_of_followers', '$twitter_audit_link', '$ethereum_wallet','$email', '');";
    $result = mysqli_query($db, $sql);

1 个答案:

答案 0 :(得分:2)

如果我没看错,您希望它创建一个用户可以看到的菜单。因此,在此示例中,用户将仅看到两个名为“添加角色”和“编辑角色”的条目,对吧?

创建一个Menù类,如下所示:

export class MenuEntities {
   public state: string;
   public name: string;
   public target: boolean;
}

创建一个MenuEntities数组来解析Json。我假设配置在以下代码行中:

children: [
              {
                state: 'add-roles',
                name: 'Add Roles',
                // target: true
              },
              {
                state: 'edit-roles',
                name: 'Edit Roles',
                // target: true
              }
            ]

创建数组并映射json res:

public menuEntities: MenuEntities[] = [];
// I let you do the logic to parse the json to this array.

一旦有了它,就可以开始了。只需将.html文件循环到该数组中,便会得到一个菜单:

<ul *ngFor="let entity of menuEntities">
   <li>States : {{entity.states}}</li>
   <li>Name : {{entity.name}} </li>
</ul>

注意:如果您发布更多代码,我可以编辑此答案,但是这种逻辑很好用