O
O
odd-look2015-09-07 16:08:59
JavaScript
odd-look, 2015-09-07 16:08:59

How to pass a function to a custom directive from a controller?

There is a custom directive for which you need to do ng-change. But only function (a) {h(c, a) comes to the directive controller. That is, my function is lost somewhere along the way.
Sample

input(
        ng-model="dt"
        ng-change='onChange'
    )

Directive Controller
.directive('uiDatepicker', [()->
    obj =
        restrict: 'AE'
        replace: true
        templateUrl: '/partials/datepicker'
        scope:
            dt: '='
            onChange: '&'
        controller: ['$scope', ($scope)->
            console.log $scope.onChange   #function (a) {h(c, a)
        ]
    obj
])

This is how I pass the function
ui-datepicker(dt='tenancy.startDate', onChange='test()')

How to see the function in the directive and make it work?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kano, 2015-09-07
@odd-look

For starters, you have the wrong attribute name
If you want to pass exactly the same function that was set in the controller, then you have chosen the wrong binding mechanism. You need pass by value, not by expression so use pass by value in your isolated scope

.directive('uiDatepicker', [()->
...
        scope:
            dt: '='
            onChange: '='
])

ui-datepicker(dt='tenancy.startDate', on-сhange='test')

S
Sergey, 2015-09-07
Protko @Fesor

$scope.onChange(); // тут можно передать контекст. Читать документацию.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question