ddd
ddd

Reputation: 219

Angularjs directive: Passing a model reference to an isolated scope

I 'm trying to write a collapsible, reusable calculator directive, that binds to an input field (in the parent scope). This input field itself has a ngModel binding. When the user presses the equals-button of my directive this parent scope model should be updated. I need to isolate the scope so I can reuse it:

Here is the simplified code and how I would like to use it: http://plnkr.co/edit/OSOcxydJWh8K520nstAU?p=preview

I tried passing in the values as an attribute. but that does not work because I don't know how to update this attribute inside of the controller(I tried the $attrs service).

So how can I update the model from the directive?

Upvotes: 1

Views: 511

Answers (2)

John Munsch
John Munsch

Reputation: 19528

Maybe you're overthinking it, maybe I'm underthinking it. Either way, here's all I did to change yours to make it work:

if ($scope.operator ==='+') {
  $scope.field = parseInt($scope.field) + $scope.operand;
}

I uncommented your scope and then I made sure that your controller made reference to the data you had exposed in your scope. That's it.

And here's a working version of your Plunker: http://plnkr.co/edit/btBi3E

Upvotes: 1

Roman Kolpak
Roman Kolpak

Reputation: 2012

You need to use ngModelController. Here's a link with docs, with a handy example: NgModelController

Upvotes: 0

Related Questions