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

Allow binding to getter/setter fn

Open vojtajina opened this issue 14 years ago • 47 comments

Now, when we have "accessor" locals, one should be able to bind to accessor. So either $parse or ng-model shoul handle it.

This is ugly - with binding to getter/setter, I wouldn't have to do the extra "value" and copying back and forward: http://jsfiddle.net/vojtajina/XuhzP/64/

The tpl of the component would be just:

<span ng-hide="editMode" ng-click="editMode=true">{{model()}}</span>
<input type="text" ng-model="model" ng-show="editMode" ng-enter="editMode=false" />

Another ugly solution (taking advantage of obj reference from parent scope): http://jsfiddle.net/vojtajina/XuhzP/65/

vojtajina avatar Mar 05 '12 01:03 vojtajina

+1

petebacondarwin avatar Mar 29 '12 20:03 petebacondarwin

+1 Would be great for a data-store with many entries to detect which entries are dirty.

tbosch avatar Apr 14 '12 19:04 tbosch

I believe the new directive scope syntax takes care of this. See "Directive Definition Object" here: http://docs.angularjs.org/guide/directive

Can I close this?

btford avatar Jul 19 '12 19:07 btford

As far as I can tell, ng-model does not yet support getter/setters. And this is what the example above is about.

Tobias

tbosch avatar Jul 24 '12 18:07 tbosch

Oops, I totally misunderstood. I get it now; thanks! I also think this would be a good feature.

So implementation-wise, we just check if ng-model is a function, and if so assume it's a getter/setter?

btford avatar Jul 24 '12 18:07 btford

Yes, kind of this. However, don't know if ng-model is the best place for this....

tbosch avatar Jul 24 '12 18:07 tbosch

Also ng-model doesn't watch for deep changes in the case that the model is an array.

On 24 July 2012 19:49, Tobias Bosch < [email protected]

wrote:

Yes, kind of this. However, don't know if ng-model is the best place for this....


Reply to this email directly or view it on GitHub: https://github.com/angular/angular.js/issues/768#issuecomment-7228348

petebacondarwin avatar Jul 24 '12 20:07 petebacondarwin

+1

pburgmer avatar Jul 27 '12 11:07 pburgmer

+1

yarosla avatar Dec 18 '12 19:12 yarosla

+1

marcorinck avatar Jan 16 '13 09:01 marcorinck

+1

saltmueller avatar Feb 01 '13 18:02 saltmueller

+1

abourget avatar Feb 12 '13 19:02 abourget

So things have changed since this ticket was opened (as ng-model-instant is no more), but couldn't we do something like:

ng-model="function()"

and ng-model would take that thing, call it when it needs to read, and call function(new_val) when things change.

Or it could pass as the first parameter, the $scope all the time.. (even in "getter" mode).. so that we have context. What do you think ?

abourget avatar Feb 12 '13 19:02 abourget

what about allowing the user to assign an ng expression {{}} in ng-model that gets evaluated to an object for two way data-binding?

orneryd avatar Feb 22 '13 02:02 orneryd

+1

santyclaz avatar Mar 08 '13 19:03 santyclaz

+1

maxbates avatar Mar 12 '13 01:03 maxbates

Really need this to write code that use third party librairies where model properties are getter/setter.

Edit: I made a workaround using ES5 get/set: http://plnkr.co/edit/c863Z267jdM7KygfBUi8

tomsdev avatar Mar 21 '13 17:03 tomsdev

:+1:

jperl avatar Mar 26 '13 22:03 jperl

:+1:

ganarajpr avatar Apr 12 '13 08:04 ganarajpr

Just ran into another checkbox use case that really needs getter/setter support...

coli avatar Apr 18 '13 22:04 coli