A
A
abusabir2015-12-25 12:31:46
JavaScript
abusabir, 2015-12-25 12:31:46

Angular: how to get data from controller of own directive?

Something I'm confused. I'm rewriting someone's terrible code in angular and the crux of the matter is this.
Has its own directive, with its own template, with its own
...

controllerAs: 'startPoint',
            controller: function($scope, $element, routePointInit) {
                $scope.point = {
                    isDisabled: false,
                    address: '',
                    input: $element[0].querySelector('input')
                };
                routePointInit($scope.point.input);
            },

There is an indication in the template of the directive
ng-model="point.address"
At the same time, attempts to get access from outside through
startPoint.point.address
For example, with a simple {{startPoint.point.address}} do not even
lead to anything.
What can be wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2015-12-25
@abusabir

In this case, it controllerAscreates a 'startPoint' variable in the directive's current scope.
If the scope of the directive is isolated or new ( scope: {}or scope: true} - then the variable will not be available from the outside.
In any case, the directive is a self-sufficient unit, and if you need to get its data elsewhere in the application, the application is thought out incorrectly.
If there is no way out, it remains to use services / factories that, by certain keys, will store data that will be available at other points in the application.
// EDIT
1) The directive should not give anything to anyone. Basically, its duty is to perform some data manipulation and display it in the view.
2) Usage$scope/$elementin the controller - mauvais ton. All DOM operations must occur in the directive's link function. If the logic is complex, then all this is taken out into services (in which, in fact, all the business logic of the application should be stored).
3) Try replacing $scope with this in your code. Read carefully about controllerAs syntax .

A
Anton, 2015-12-25
@sHinE

As far as I remember, the directive parameters use the scope key, which specifies an object that regulates how the binding of the directive parameters will work - one-way / two-way, etc.
habrahabr.ru/post/180365

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question