1

The directive bellow return an object "directive". Can I return it to a function?

var sharedApp = angular.module("sharedApp", []);
sharedApp.directive("showAvatar", function () {
    var directive = {
        restrict: "ACEM",
        replace: true,
        template: "<h2>This is directive template of shared module</h2>"
    };

    return directive;
});

(Updated)For example

var sharedApp = angular.module("sharedApp", []);
sharedApp.directive("showAvatar", function () {
    function directive(){
        this.restrict = "ACEM";
        this.replace = true;
        this.template = "<h2>This is directive template of shared module</h2>";
    }

    return directive;
});
4
  • What do you mean by "return it to a function" ??? Commented Jun 3, 2014 at 8:38
  • Hi ExpertSystem. Please see my example! Commented Jun 3, 2014 at 8:59
  • It won't work like this. Why do you want to return a function ? BTW, have you read the documentation ? Commented Jun 3, 2014 at 8:59
  • What are you trying to do? Can you give us an example of how you want to use the directive? Commented Jun 3, 2014 at 9:03

1 Answer 1

2

Return it to a function means do you want to return directive object from your directive to JS controller..

Define your Directive like this

App.directive('showAvatar', function() {
return {
    restrict : 'EA',
    scope : {
        returnFunction : '&'
    },
    controller : function($scope) {
       $scope.directive = {
        restrict: "ACEM",
        replace: true,
        template: "<h2>This is directive template of shared module</h2>"
      };
    $scope.returnFunction({data : $scope.directive});
   }

});

Write your directive html tag like this

<show-avathar return-function = "getDataFromDirective(data)">

And finally in your controller, declare the function getDataFromDirective()

$scope.getDataFromDirective = function(data){
            alert(data);
        }
Sign up to request clarification or add additional context in comments.

Comments

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.