0

I am sending a controller method to directive. I want to get parameter frpm direvtive like events.

DEMO

var app = angular.module("app", []);

app.controller("outCtrl", function($scope){
        $scope.callOut = function(pramFromDirective){
        console.log(pramFromDirective);
    }
})


app.directive("myDir", function(){
  return {
            restrict: "E",
            scope: {
                call: "&"
        },
            controller: function($scope){
            $scope.call({message: 444});
        }
  }
})

I want to send a parameter to outCtrl.callOut() method. But it does not.

2 Answers 2

1

You have to add the parameter in the template, with the same name as the property of the object you pass to $scope.call(). Since you call it as $scope.call({message: 444}) do:

<my-dir call="callOut(message)"></my-dir>
Sign up to request clarification or add additional context in comments.

2 Comments

But a developer that using my directive can not know my inner parameter name in this stuation. He can send a wrong name
This is indeed a problem, but a different one. Can be solved with documentation or code conventions. Angular is doing it through documentation (see the $event argument of e.g. ngClick - check out the "Arguments" section).
0

Your template isn't quite correct. You need to have the parameter in your callback method binding match the name of the object key you use in your directive match exactly.

<div ng-app="app">
  <div ng-controller="outCtrl">
    <my-dir call="callOut(message)"></my-dir>
  </div>
</div>

Angular isn't actually passing the parameter straight through. It maps the object keys from your directive function call parameter $scope.call({ message: 444 }) to parameter names of the method in the template call="callout(message)".

1 Comment

But a developer that using my directive can not know my inner parameter name in this stuation. He can send a wrong name.

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.