Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat(ngModel): add public $setModelValue fn to programmatically update model #11128

Closed
tdakhla opened this issue Feb 22, 2015 · 4 comments
Closed

Comments

@tdakhla
Copy link

tdakhla commented Feb 22, 2015

In regards to my workaround in #11090 (comment), it would be nice to have a $setModelValue fn in NgModelController which would programmatically update the model. Instead of having to inject in $parse to my directive, do $parse(attrs.ngModel).assign(scope, newValue), etc, (I've seen code like this in several places, and I've used it a lot in my directives), there should be a public fn to do that work.

The code in NgModelController should be as easy as:

this.$setModelValue = function(value) {
    ngModelSet($scope, value);
};

Or, if all the $viewChangeListeners need to get executed, then perhaps open up this.$$writeModelToScope by changing it so it accepts a model value param, and rename it to this.$setModelValue (and doing the handful of refactors). I think having $setModelValue would be a nice addition to the ngModel API.

@tdakhla tdakhla changed the title feat(ngModel): add public $setModelValue fn to programmatically update mdoel feat(ngModel): add public $setModelValue fn to programmatically update model Feb 22, 2015
@Narretz Narretz added this to the Ice Box milestone Feb 25, 2015
@Narretz
Copy link
Contributor

Narretz commented Feb 25, 2015

Customizing when / how the model is set to scope is definitely planned. Btw, $setModelValue is a bit confusing as there's also the $modelValue property on ngModel, and I would expect that this is set from this function.

@realityking
Copy link
Contributor

I've got an implementation in #10764, I just haven't had time to adress the last comment.

@maraisr
Copy link

maraisr commented Feb 13, 2017

+1

@Narretz
Copy link
Contributor

Narretz commented Sep 29, 2017

Setting the scope model from inside the ngModel controller so that it runs the model > view pipeline again is not good practice. There might be edge cases where it is necessary, but I'd rather have devs use parse and assign in this case, then expose a function on the ngModelController that does it.
For the simple case where you need to re-run the formatters on the modelValue, there will be a function in 1.6.7: 1451948

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

4 participants