Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

ng-model not updating with 1.3rc3 #243

Open
brianchance opened this issue Sep 30, 2014 · 25 comments
Open

ng-model not updating with 1.3rc3 #243

brianchance opened this issue Sep 30, 2014 · 25 comments

Comments

@brianchance
Copy link

Using angular 1.3 rc3, my ng-model is never updated.

Code...

<ui-select multiple ng-model="vm.model.roles">
    <ui-select-match placeholder="Select Roles...">{{$item.name}}</ui-select-match>
    <ui-select-choices repeat="role.roleId as role in vm.roles">{{role.name}}</ui-select-choices>
</ui-select>

When the call to ngModel.$setViewValue is made, the ngModel.$viewValue is already the newValue (as well as ngModel.$$lastCommittedViewValue).

Tracing through $setViewValue to $$debounceViewValueCommit, then to $commitViewValue, it returns without parsing and validating if $$lastCommittedViewValue === viewValue.

Not sure why the ngModel already has the newValue. I see your note about the 3rd parameter forcing revalidate, but that is not there in 1.3 rc3. Maybe a call to ngModel.$$validate?

Side question - what is the preferred method of validating contents like other input elements?

@zavidovych
Copy link

I have the same problem. Using ng-model="$parent.model" doesn't help either.

@leandroz
Copy link

what has the $item variable?

@brianchance
Copy link
Author

I am not sure, I was guessing something in ui-select-match. I was following this example: http://plnkr.co/edit/juqoNOt1z1Gb349XabQ2?p=preview (Array of objects with single property binding)

@brianchance
Copy link
Author

The example and my code did work with 1.2.25

@brianchance
Copy link
Author

More/better information.. vm is a "Controller as vm" syntax, the vm.model.roles is an array of int [2]and the vm.roles is an array of objects

[{"roleId":2,"name":"Client Admin"},{"roleId":4,"name":"Client Auditor"},{"roleId":3,"name":"Client User"}]

@zavidovych
Copy link

$select.ngModel.$modelValue doesn't get updated (it gets updated in the example here though: http://plnkr.co/edit/juqoNOt1z1Gb349XabQ2?p=preview)

@klauspost
Copy link

For me it only happens with the "multiple", not with single-select lists.

@zavidovych
Copy link

getting rid of extra $formatters from ngModel provided a quick dirty fix to our specific usecase.
https://github.com/angular-ui/ui-select/blob/master/src/select.js#L597

@klauspost
Copy link

I couldn't really make anything work there, but inserting a "scope.mymodel.myvalue = newValue" in https://github.com/angular-ui/ui-select/blob/master/src/select.js#L735 fixes it for me, although that isn't really a feasible solution.

@brianchance
Copy link
Author

I changed

ngModel.$setViewValue(newValue, null, true);

to

ngModel.$setViewValue(newValue);
if (ngModel.$validate) 
     ngModel.$validate();

@klauspost
Copy link

Much better, and works here as well!

@dimirc
Copy link
Contributor

dimirc commented Oct 10, 2014

Related to merged #193
Can you check if all is working now?

@brianchance
Copy link
Author

Tested with 1.3 rc4, works, thank you.

@nicolasbinet
Copy link

I had the same problem. Tested with 1.3 rc5 and works as well.
thank you.

@tannerlinsley
Copy link

From a duplicate/related issue (#205):

Saw this issue in production after upgrading to angular 1.3.4, Everything multiple looks okay, but model never updates.

Here is a plunker: http://plnkr.co/edit/VdC2ImYaQQJQPw1jsDUV?p=preview
The styles in the plunker are a bit messed up, but the multiple select binding is clearly broken

@bogacg
Copy link

bogacg commented Dec 23, 2014

I'm having same issue @tannerlinsley stated, with multiple and on Angular 1.3.4.
@brianchance 's code does sometimes work, sometimes doesn't:

ngModel.$setViewValue(newValue);
if (ngModel.$validate) 
     ngModel.$validate();

yet if it works it gives an error with filter.
UPDATE: That code only works for first execution, after any refresh model is not updated again.

Any non-breaking fix would be appreciated.

@MWalid
Copy link

MWalid commented Jan 5, 2015

Same issue...cannot get selected model with Angular 1.3.4

Unfortunately using native select till this issue being solved...

@tannerlinsley
Copy link

Any word on this? 👍 💯

@jasdeepsingh
Copy link

I'm experiencing this same issue with Angular 1.3.2 and ui-select 0.10.0 - 2015-02-26T06:35:06.239Z

@dimirc
Copy link
Contributor

dimirc commented Mar 17, 2015

After #748 merged, can you check if all good now?

@abeninskibede
Copy link

Not working for me (tried with multiselect)

@abeninskibede
Copy link

Hi guys,

I went throught angular docs about ngModelCtr.$setViewValue saying:

If the new value is an object (rather than a string or a number), we should make a copy of the object before passing it to $setViewValue. This is because ngModel does not perform a deep watch of objects, it only looks for a change of identity. If you only change the property of the object then ngModel will not realise that the object has changed and will not invoke the $parsers and $validators pipelines.

So I found the code where the model is updated when an item is selected in the dropdown and changed it:

from

ctrl.updateModel = function(){
        ngModel.$setViewValue(Date.now()); //Set timestamp as a unique string to force changes
        ctrl.refreshComponent();
      };

to

ctrl.updateModel = function () {
                    var newVal = $select.selected;

                    if (angular.isArray(newVal) || angular.isObject(newVal)) {
                        newVal = angular.copy($select.selected);
                    }

                    ngModel.$setViewValue(newVal); //Set timestamp as a unique string to force changes
                    ctrl.refreshComponent();
                };

This seems to work for me. Hope it helps you.

@wearetelescopic
Copy link

Still having this issue with multiple selects - but getting rid of $formatters as per @zavidovych worked here too.
The solution from @abeninskibede didn't work for me.

@lesliesu
Copy link

Still facing this issue with multiple selects - have used with angular 1.4.7.
my ui-select was initially hided, then need show up & be able to updates its options while model changed.

Strange thing to me is only the first time the options of my ui-select wasn't set up(only showed up a blank input box) while model been updated; after that, its options DO update as model changes.

@wesleycho
Copy link
Contributor

Can someone post a Plunker based on an up to date version of UI Select?

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

No branches or pull requests