I know that this probably have been answered several times, and it might even be pointed out in Angular documentation, but I can't for my life understand how to do this 'correct'.
I have a directive that list items in a table with user defined objects. I want the items to be synced between the directive and the scope it resides in (for example if items are added or removed from the parent scope, I want the list to be updated accordingly. The same goes the other way around; if items are selected in the list, I want the parent to know this.) At the end, I want it to be shared memory, so that the data just 'works', without $watch, $apply & $dispatch, if that is possible.
I thought I could do it like this:
Directive:
{
restrict: 'E',
templateUrl: 'js/components/selectionList/component.selectionList.html',
controller: 'SelectionlistCtrl',
controllerAs: 'selecListCtrl',
scope: {
items: '='
},
link: (scope, element, attrs) => {
scope.selecListCtrl.items = scope.items;
}
}
HTML:
<selection-list items="vm.items" />
vm is the controller in the parent scope, and items is an array of objects. This kinda works, but as soon as I leave the view that contains the selection-list directive, the list items are not re-loaded when I enter the view again (this is probably because link(...) function only runs once and the directive is then cached).
I can ensure that the view and directive aren't cached, and then it works. But it must be a better way to do this? Or am I out of luck?