roc*_*k11 3 typescript angular-material angular
我的应用程序中有一个基本的滑动切换功能。
我想根据切换是否切换来更改滑动切换的文本。切换按钮时基本上会显示不同的文本。
我尝试使用幻灯片切换的toggleChange() 方法,但重新加载时无法显示该文本。
此外,由于该函数是在切换更改时调用的,因此即使在触发切换事件时切换进入关闭状态,它也会显示文本。我想在切换按钮时更改文本,但仅在单击保存设置按钮时保存切换的文本。我想我必须使用会话或本地存储来存储文本消息
我现在似乎不知道该怎么办。
请帮忙。
提前致谢。
这是我的 ts 代码
export class PolicyComponent implements OnInit {
formGroup: FormGroup;
policy3: boolean=false;
policy4: boolean=false;
message:string="Message1";
test:boolean=false;
disable:boolean=true;
serverData:any
constructor(
private formBuilder: FormBuilder,private policyService:PolicyService
) { }
ngOnInit() {
this.formGroup = this.formBuilder.group({
Policy1: new FormControl(false, []),
Policy2: new FormControl(false, [])
});
this.policyService.getPolicy().subscribe((response) => {
this.serverData = response[0];
if(this.serverData.policy1=="F")
{
this.policy3=false;
}
else this.policy3=true;
if(this.serverData.policy2=="F")
{
this.policy4=false;
}
else this.policy4=true;
this.formGroup.controls.Policy1.setValue(this.policy3);
this.formGroup.controls.Policy2.setValue(this.policy4);
});
// if(this.formGroup.controls.Policy1.value)
// {
// this.message="Message1";
// }
// else if(!this.formGroup.controls.Policy1.value){
// this.message="Message2";
// }
// this.policyService.getUserById(+ localStorage.getItem("policyId")).subscribe();
}
onFormSubmit() {
console.log("Policy1::"+this.formGroup.controls.Policy1.value);
if(this.formGroup.controls.Policy1.value)
{
this.serverData.policy1="T";
}
else{
this.serverData.policy1="F";
}
console.log("Policy2::"+this.formGroup.controls.Policy2.value);
if(this.formGroup.controls.Policy2.value)
{
this.serverData.policy2="T";
}
else{
this.serverData.policy2="F";
}
//this.policyService.updatePolicy(this.policy.id,{policy1:this.policy.policy1}).subscribe(err=>{console.log(err)});
this.policyService.updateUser(this.serverData).subscribe(err=>{console.log(err)});
// alert(JSON.stringify(formValue, null, 2));
this.disable=true;
}
onChange(ob: MatSlideToggleChange) {
// this.policy3 = !this.policy3;
this.disable=false;
}
onChange1(ob: MatSlideToggleChange) {
// this.policy4 = !this.policy4;
this.message="Slide";
this.disable=false;
}
displayMessage()
{ console.log("Value is:"+this.formGroup.controls.Policy1.value);
if(!this.formGroup.controls.Policy1.value)
{
this.message="Message1";
}
else if(this.formGroup.controls.Policy1.value){
this.message="Message2";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模板代码:
<form class="example-form" [formGroup]="formGroup" (ngSubmit)="onFormSubmit()" ngNativeValidate>
<mat-action-list>
<mat-list-item > <mat-slide-toggle
(change)="onChange($event)" (toggleChange)="displayMessage()" formControlName="Policy1" >{{message}}</mat-slide-toggle></mat-list-item>
<mat-list-item > <mat-slide-toggle (change)="onChange($event)" formControlName="Policy2">Policy2</mat-slide-toggle></mat-list-item>
</mat-action-list>
<p>Form Group Status: {{ formGroup.status}}</p>
<button mat-rasied-button [disabled]="disable" type="submit">Save Settings</button>
</form>
Run Code Online (Sandbox Code Playgroud)
小智 6
和另一个解决方案
<mat-slide-toggle #slide formControlName="enabled">
<span>{{slide.checked?'label 1':'label 2'}}</span>
</mat-slide-toggle>
Run Code Online (Sandbox Code Playgroud)