Topic: Material Select Issues with binding
stgiaf pro asked 6 years ago
nthieu13 pro answered 6 years ago
I got the same issue. Look at my sample code:
this.service.getData().subscribe(resp => { // for demo ignore the resp, bring this hardcode options outside the subscribe block and observe the 2-way binding works. this.options = [{ value: 1, label: 'Today'}, { value: 2, label: 'Yesterday' }, { value: 3, label: 'Last 7 days' }, { value: 4, label: 'Last 30 days' }, { value: 5, label: 'Last week' }, { value: 6, label: 'Last month' }]; }); }
The above code is in ngOnInit() and mdb-select is used in a Reactive Form. I believe mdb-select has not managed its state correctly against async changes. Additionally, the arrow is missing, adding 'z-index: 1' in global css makes the arrow appears but clicking on that arrow opens the option list and close it immediately. The label not floating like input text which I have to make a wrapper for it. I hope the mdb-select will be like https://material.angular.io/components/select/overview in future release.
Damian Gemza staff commented 6 years ago
Dear nthieu13, which problem do you have with your code?itdev pro commented 6 years ago
I am having the same issue. The Arrow does not appear and the selected value does not work when changes are made within an observable.Damian Gemza staff answered 6 years ago
<div class="row"> <div class="col-md-6 mx-auto my-5"> <mdb-select [(ngModel)]="choosen.department.id" [options]="optionsSelect" placeholder="Choose your option"></mdb-select> <label>Example label</label> </div> </div> <div class="row"> <divclass="col-md-6 mx-auto my-5"> <div> OPCJA: {{choosen.department.id}} </div> </div> </div>
optionsSelect: Array<any>; privatechoosen= { id:0, title:'', department: { id:0, } }; url='https://jsonplaceholder.typicode.com/posts'; constructor(privatehttp:Http) { } getData() { returnthis.http.get(this.url).map(res=>res.json()); } ngOnInit() { this.getData().subscribe(next=> { console.log(next); this.optionsSelect= [ { value: next[0].id, label: next[0].title }, { value: next[1].id, label: next[1].title }, { value: next[2].id, label: next[2].title }, { value: next[3].id, label: next[3].title }, { value: next[4].id, label: next[4].title }, { value: next[5].id, label: next[5].title }, ]; }); } }
Damian Gemza staff answered 6 years ago
stgiaf pro commented 6 years ago
Hello Damian, No this is something different, with material-selectDamian Gemza staff commented 6 years ago
Dear stgiaf, I've tried to reproduce your error following closely your steps, but for me, everything is working as expected. Could you tell me one more time, which thing doesn't work for you well, in which way works now, and what is your expected behavior of this thing? Best Regards, Damianstgiaf pro commented 6 years ago
Hello Damian, Did you try to recieve the data via a service asynchronously, and the behavior of your material select was the same as a normal select? I my case, when everything is completed the variable that ngModel is bound to is "undefined" , although from the service it comes correctly and also the values exists in the [options] input. This happens, possibly because at the time of rendering the control, the observable has not yet been completed and then the values are lost somehow. if you remove two way binding and do it with one way it works. The issue here is that mdb material select works differently from normal select element. If you have a working example of material select with values coming from a service (http) via objssrvable and values bound to a model of yours with ngModel, then supply it to me , so that I can replicate.FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- ForumUser: Pro
- Premium support: No
- Technology: MDB Angular
- MDB Version: -
- Device: -
- Browser: -
- OS: -
- Provided sample code: No
- Provided link: No