angular - Angular 获取 URL 参数并将它们写入输入字段
问题描述
我有一个系统,其中有一个更改密码组件。这里的问题是我的来自 resetPW 的 URL 参数进入了第一个输入字段。
这是我的component.html:
<div class="container">
<h3>Reset Password</h3>
<form (ngSubmit)="resetPassword(f)" #f="ngForm" ngNativeValidate>
<div class="form-group">
<input type="password" minlength="8" class="form-control" placeholder="Nyt
Password" id="newPassword" name="newPassword" [(ngModel)]="newPassword"
charracters minimum required autofocus>
</div>
<div class="form-group">
<input type="password" minlength="8" class="form-control"
placeholder="Gentag Nye Password" id="repeatPassword" name="repeatPassword"
[(ngModel)]="repeatPassword" charracters minimum required autofocus>
</div>
<button class="button" type="submit">Change password</button>
</form>
<br>
<p>{{notChangedPassword}}</p>
</div>
这是我的component.ts:
export class ResetPWComponent implements OnInit {
@Input("newPassword") newPassword;
@Input("repeatPassword") repeatPassword;
notChangedPassword: string;
urlParams;
paramsArray = [];
constructor(private authService: AuthService, private activatedRoute:
ActivatedRoute) {
this.newPassword = new FormGroup({
newPassword: new FormControl("newpassword", Validators.compose([
Validators.required,
Validators.pattern("^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$")
])),
repeatPassword: new FormControl("repeatPassword", Validators.compose([
Validators.required,
Validators.pattern("^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$")
])),
})
this.activatedRoute.queryParams.subscribe(params => {
this.urlParams = params["reset"];
})
this.authService.notChangedPassword.subscribe((data) => {
this.notChangedPassword = data;
})
}
ngOnInit() {
this.paramsArray = this.urlParams.split(".", 4);
}
resetPassword(form: NgForm) {
const token = this.paramsArray[0] + "." + this.paramsArray[1] + ".";
const email = this.paramsArray[2] + "." + this.paramsArray[3];
const newPassword = form.value.newPassword;
const repeatPassword = form.value.repeatPassword;
this.authService.changePassword(token, email, newPassword, repeatPassword);
}
}
出于某种原因,此行确实将 url 参数放在输入字段中:
this.activatedRoute.queryParams.subscribe(params => {
this.urlParams = params["reset"];
})
我不明白为什么会这样,有人可以帮忙吗?
解决方案
推荐阅读
- java - 我相信由于范围,这不是我想要的方式。有没有办法让这个工作?
- mysql - 我在 Laravel 中使用 eloquent 找到什么相关模型?
- aws-lambda - API Gateway 的 Lambda 集成能否返回自定义对象?
- zsh - 更改 ZSH 命令提示符
- angularjs - 查询参数的顺序对于 Angular UI 路由器应该无关紧要
- javascript - 如何将值数组传递给自动完成选项
- ios - 如何让 dispatch_async 运行
- c++ - CMake:将 GTest 添加到构建
- reactjs - 更新“URL”但不加载路径
- python - Pandas Dataframe:如何通过每个代理和堆叠代理的多个观察来区分观察之间的差异