A
A
arentyev2014-11-10 19:50:24
Angular
arentyev, 2014-11-10 19:50:24

What is the best way to bind data between controller and directive in AngularJs?

There is a task to write a directive that would change the value in the external input'e.
I tried to solve the problem by linking the controller variable with the directive variable through the directive attribute, followed by scope:{var: "="} - jsbin.com/mifaqi/1/edit?html,js,output but apparently it only works in one direction. When updating the data in the directive, they do not change in the controller.
Are there any other adequate options, or is it only left to search for input by id in the link function and change its value?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
roman450, 2014-11-10
@aterentyev

After regular js events, you need to use scope.$apply() or scope.$digest(): jsbin.com/gigewepuxi/1/edit In
general, it's better to use the ng-click directive in such cases.

S
Sergey Romanov, 2014-11-11
@Serhioromano

There is a small trick. If it is an object, then it should change. For example in directive. attr="var" where var is an object. And if you then have scope:{attr: "="} in the directive and in the linker scope.attr.somethign = 1; The result may be quite different.
In general, no object should work in 2 directions. Have you tried using controller instead of link?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question