【问题标题】:Navigating to a specific page based on radio button checked in Angular根据 Angular 中选中的单选按钮导航到特定页面
【发布时间】:2020-11-19 21:28:31
【问题描述】:

我正在尝试为普通用户和管理员制作一个简单的登录应用程序。我做了一个有效的登录功能,但我希望用户选择他是普通用户还是管理员。我想为此使用单选按钮。根据单击的单选按钮,登录后,我希望用户导航到相应的页面。因此,如果用户选中“用户”单选按钮,我希望他转到专门针对用户的页面,如果用户选中“管理员”单选按钮,我希望他在登录后导航到专门针对管理员的页面。

login

这是一段正在登录的代码,用户在登录后会被导航到家。

    if(this.checkCredentials(signInData)) {
      this.isAuthenticated = true;
      this.router.navigate(['home']);
      return true;
    }
    this.isAuthenticated = false;
    return false;
  }

这是我的单选按钮所在的 HTML 代码

                    <div class="custom-control custom-radio">
                      <input id="user" name="usertype" type="radio" class="custom-control-input" ng-model="userischecked"checked required>
                      <label class="custom-control-label" for="admin">User</label>
                    </div>
                    <div class="custom-control custom-radio">
                      <input id="admin" name="usertype" type="radio" class="custom-control-input" required>
                      <label class="custom-control-label" for="admin">Admin</label>
                    </div>
                    <div class="custom-control custom-radio">

登录发生在身份验证服务中,单选按钮位于登录组件 HTML 文件中,如果有帮助的话。

编辑:我附上了我的整个 HTML 代码:

<div class="login-wrapper">
    <div class="card">
        <div class="card-body">
            <form class="form-signin"  (ngSubmit)="onSubmit(signInForm)" #signInForm="ngForm">
                <h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
                
                <label for="inputID" class="sr-only">ID</label>
                <input type="id" id="inputID" name="id" class="form-control" 
                placeholder="ID" required autofocus ngModel>
                
                <label for="inputPassword" class="sr-only">Password</label>
                <input type="password" id="inputPassword" name="password" class="form-control" 
                placeholder="Password" required ngModel>
                
                <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>

                <div class="d-block my-3">
                    <div class="custom-control custom-radio">
                      <input id="user" name="usertype" type="radio" class="custom-control-input" ng-model="userischecked"checked required>
                      <label class="custom-control-label" for="admin">User</label>
                    </div>
                    <div class="custom-control custom-radio">
                      <input id="admin" name="usertype" type="radio" class="custom-control-input" required>
                      <label class="custom-control-label" for="admin">Admin</label>
                    </div>
                    <div class="custom-control custom-radio">
                      
                    </div>
                  </div>
              </form>
        </div>
    </div>
</div>

【问题讨论】:

    标签: angular radio-button angular10


    【解决方案1】:

    HTML 代码

     <label id="example-radio-group-label">Select your role</label>
     <mat-radio-group aria-labelledby="example-radio-group-label" class="example-radiogroup" [(ngModel)]="selectedRole">
     <mat-radio-button class="example-radio-button" *ngFor="let role of rolesAvailable" [value]="role">
    {{role}}
    </mat-radio-button>
    </mat-radio-group>
    <div>Your role is: {{selectedRole}}</div>
    

    TS 代码

    selectedRole: string;
    rolesAvailable: string[] = ['Admin', 'User'];
    
    login(){
     console.log(this.selectedRole)  //Will give you the role selected;
     if(this.selectedRole=="Admin"){
        this.router.navigate(['admin'])
     }
     else if(this.selectedRole=="User"){
       this.router.navigate(['user'])
     }
    }
    

    Check this

    没有材料 HTML 的代码

    <input type="radio" value="Admin" [(ngModel)]="selectedRole">Admin
    <input type="radio" value="User" [(ngModel)]="selectedRole">User
    

    【讨论】:

    • 我应该用你的覆盖我的单选按钮 HTML 代码吗?因为当我这样做时,我的页面变得完全空白。我将在问题中附上我的整个 HTML 代码。
    • ng add @angular/material - 在项目中添加材料 ||从“@angular/material/radio”导入 {MatRadioModule}; - 在 app.module 中导入这个 ||在底部有没有材料的代码,应该适合你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2018-12-13
    • 2018-10-04
    • 2013-04-09
    • 2018-03-14
    • 2016-05-12
    相关资源
    最近更新 更多