angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Angular.js v1.3.0-rc.0 directive ngModelController $viewValue always string

Open alonn24 opened this issue 12 years ago • 9 comments

With latest angular 1.3-rc release, encounter a problem with directives.

  1. passing ng-model to directive with object values
  2. adding to directive ng-model and require ng-model
  3. getting ngModelController.$viewValue as string ["Object object", "Object object"]

alonn24 avatar Aug 31 '14 09:08 alonn24

Yes, this was changed recently. How do you normally handle your object models? The standard way is to have a formatter that formats your object as a string. Can you give an example (plunkr) of your use case? To clarify, as the $viewValue should represent what the user sees, it makes sense that it is always a string.

Narretz avatar Aug 31 '14 12:08 Narretz

The other formatters come after the default one (toString), so you cannot format the object anymore.

pdanpdan avatar Sep 02 '14 13:09 pdanpdan

Ah, I see have. Have you tried unshifting your formatter in the $formatters? Then it should run before the string formatter.

Narretz avatar Sep 02 '14 19:09 Narretz

@pdanpdan: If you use a directive with a higher than default priority and use $formatters.push(yourFormatter) in the post-link function, it should be applied first.

@Narretz: Since $formatters are applied in the reverse order, you need to push() it (so it gets applied first) and you should also make sure that your push() happens after the input directive's push()

gkalpak avatar Sep 02 '14 20:09 gkalpak

Thank you for the solution. I just checked and it solves the problem of displaying the value, but the field does not validate (if it is required.

pdanpdan avatar Sep 02 '14 22:09 pdanpdan

@pdanpdan Please post a minimal reproducible example in a plunker etc.

Narretz avatar Sep 03 '14 10:09 Narretz

No, that sounds like a separate issue --- please file a different bug about that with a reproduction, thanks.

caitp avatar Sep 03 '14 12:09 caitp

I don't think we want to consider this a regression, because we can't support viewValues which are not strings --- it would break core validators. @matsko / @shahata feel free to reopen if you think I"m wrong here

caitp avatar Sep 03 '14 12:09 caitp

Yes that's true, anything that comes from input type: empty, text, url or email must be handled as a string value since element.value always expects them to be a string.

matsko avatar Sep 03 '14 15:09 matsko