3

I have a directive that allows to bind some attributes in a two-way manner (myDirective can also set someProp):

<my-directive some-prop="myModel.someValue" />

Is there a way similar to filters that allows to transform the model value to a view value and back in the binding expression, like this:

<my-directive some-prop="myModel.someValue | myTwoWayConverter" />

What I've understood so far: usually, such forth and back conversion is done using ngModels formatter and parser chain, but I have no ngModel in this case and would prefer a syntax that is similar to the one used for filters.

Benjamin
  • 1,165
  • 7
  • 19
  • Similar question has been aswered here: http://stackoverflow.com/a/27432203/2352017 – Pablo Jan 22 '15 at 13:42
  • @PabloDeNadai: similar, but not the same. More in the direction of my question goes the [answer](http://stackoverflow.com/questions/11616636/how-to-do-two-way-filtering-in-angular-js) that is linked there. But still it is not quite the answer, as it suggests to add a directive attribute (instead of a pipe-separated filter name) that hooks into ngModel's formatter/parser chain. It is limited to a directive using just one attribute the model could be bound to (ngModel). – Benjamin Jan 22 '15 at 15:32
  • 1
    The problem is that Angular doesn't really have a concept of a "two-way" filter. Filters are simply functions in a convenient namespace. – Chris Bouchard Jan 27 '15 at 01:07

1 Answers1

-1

You could have your directive use ngModel, then take advantage of the formatters pipeline. Read here for an example of a custom directive using ngModel. It's a lot of work for maybe not much pay-off. Maybe just use $scope.$watch?

Chris Bouchard
  • 806
  • 7
  • 12
  • Sorry, I don't see the point why this should answer my question. I'm well aware of `ngModel` and it's limitations. – Benjamin Jan 27 '15 at 09:44