0

I cannot figure out why my directive is not calling my callback function from my parent page. Can you please assist?

Angular code:

    angular
        .module('myApp')
        .directive('testDirective', function () {
            return {
                bindToController: {
                    foo: '@',
                    myCallback: '&'
                },
                controller: function () {
                },
                controllerAs: 'vm',
                restrict: 'E',
                replace: true,
                scope: {},
                template: '<div>Foo is: {{ vm.foo }} <button data-ng-click="vm.myCallback()">Click to call Callback</button></div>'
            };
        });

    angular
        .module('myApp')
        .controller('ParentController', function () {
            this.myParentCallback = function () {
                alert('Called the parent callback function');
            };
        });

HTML code:

<body>
    <div data-ng-app="myApp" data-ng-controller="ParentController as ctrl">
        <test-directive foo="bar!" my-callback="ctrl.myParentCallBack"></test-directive>
    </div>
</body>

As you can see, foo binds correctly, but I can't get myCallback to work...

Thoughts?

Here it is in plnkr: (http://plnkr.co/edit/niVL5iAeOJ6XTpkpL9fu?p=preview)

2
  • 1
    check this my-callback="ctrl.myParentCallBack()" . not sure. Commented Oct 9, 2015 at 14:31
  • Thanks @Roy.23. This refers to the function myParentCallback which exists in ParentController. Commented Oct 9, 2015 at 14:34

2 Answers 2

1

It's a case problem: pass ctrl.myParentCallback with lowercase b in the callback.

Also, your plnkr has a bug, with vm.myParentCallBack. Replace with:

<test-directive foo="bar!" my-callback="ctrl.myParentCallback()"></test-directive>

http://plnkr.co/edit/VnMUekTSZyk5WagsPqkL

Sign up to request clarification or add additional context in comments.

Comments

0

You need to actually call the myParentCallBack method on your ParentController. Like so:

<test-directive foo="bar!" my-callback="ctrl.myParentCallBack()"></test-directive>

This enables you to pass on $event or such as you wish.

Edit: You also need to change your testDirective definition:

return {
    bindToController: true,
    controller: function () {},
    controllerAs: 'vm',
    restrict: 'E',
    scope: {
        foo: '@',
        myCallback: '&'
    },
    template: '<div>Foo is: {{ vm.foo }} <button data-ng-click="vm.myCallback()">Click to call Callback</button></div>'
};

Changes are that you just tell that you want to use bindToController (by specifying it with true) and are specifying the properties with the scope property of your directive definition. You mixed them up.

4 Comments

Thanks @BasSlagter. I tried this, however it did not work. I didn't receive any error either. Any thoughts?
Thanks @BasSlagter. I replaced my code with yours exactly, and still it does not work. Any thoughts here?
Maybe you can create a fiddle/plnkr?
Thanks @BasSlagter. Here it is: (plnkr.co/edit/niVL5iAeOJ6XTpkpL9fu?p=preview)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.