Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select组件指定值时不能切换 #5618

Closed
Acessss opened this issue Jul 27, 2020 · 3 comments
Closed

Select组件指定值时不能切换 #5618

Acessss opened this issue Jul 27, 2020 · 3 comments
Assignees

Comments

@Acessss
Copy link

Acessss commented Jul 27, 2020

Reproduction link

https://stackblitz.com/edit/angular-pea3sc

Steps to reproduce

nz-select组件在ngModelChange时,手动指定model值

What is expected?

nz-select组件在ngModelChange时,可以手动指定model的值

What is actually happening?

model值不能在change时手动指定,值只能是被选中的项目

Environment Info
ng-zorro-antd 9.3.0
Browser chrome

@component({
selector: 'nz-demo-select-basic',
template: <nz-select [(ngModel)]="selected" (ngModelChange)="valueChange($event)"> <nz-option nzValue="jack" nzLabel="Jack"></nz-option> <nz-option nzValue="lucy" nzLabel="Lucy"></nz-option> <nz-option nzValue="zoe" nzLabel="zoe"></nz-option> </nz-select> <button (click)="setValue()">test</button>,
styles: [
nz-select { margin: 0 8px 10px 0; width: 120px; }
]
})
export class NzDemoSelectBasicComponent {
selected = 'zoe'

valueChange(value) {
console.log(value)
this.selected = 'lucy';
}

setValue() {
this.selected = 'lucy';
}
}

@zorro-bot
Copy link

zorro-bot bot commented Jul 27, 2020

Translation of this issue:

Select the specified value can not switch the component

Reproduction link

Https://stackblitz.com/edit/angular-pea3sc

Steps to reproduce

nz-select components when ngModelChange, manually specified value model

What is expected?

nz-select components when ngModelChange, the model values ​​may be specified manually

What is actually happening?

model value can not be manually at change, value can only be selected item

Environment Info
Ng-zorro-antd 9.3.0
Browser chrome

@component ({
selector: 'nz-demo-select-basic',
template: <Nz-select [(ngModel)] = "selected" (ngModelChange) = "valueChange ($ event)"> <Nz-option nzValue = "jack" nzLabel = "Jack"> </ nz-option> <Nz-option nzValue = "lucy" nzLabel = "Lucy"> </ nz-option> <Nz-option nzValue = "zoe" nzLabel = "zoe"> </ nz-option> </ Nz-select> <Button (click) = "setValue ()"> test </ button>,
styles: [
nz-select { margin: 0 8px 10px 0; width: 120px; }
]
})
export class NzDemoSelectBasicComponent {
selected = 'zoe'

valueChange (value) {
console.log (value)
this.selected = 'lucy';
}

setValue () {
this.selected = 'lucy';
}
}

@Acessss
Copy link
Author

Acessss commented Jul 31, 2020

请问这个问题什么时候能解决呢

@vthinkxie
Copy link
Member

vthinkxie commented Jul 31, 2020

这个和组件没有任何关系,在modelChange中重新改变model值是错误用法

  1. 如果你希望这种写法能生效,就相当于在同一个周期中写了一个死循环
    ngModel的修改 -> ngModelChange 的触发
    ngModelChange的触发->ngModel 的修改
  2. 实际情况是,在第一次 ngModelChange 触发之后,ngModel 的值永远都是你在 ngModelChange 中赋予的那个值,对于组件而言所有的 ngModel 都未发生改变,自然不会进行任何变更
    https://stackblitz.com/edit/angular-pea3sc-1sqnhu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants