0

I created a custom directive in angular. I would like to pass parent data through the directive using scope but I'm getting 'undefined' when I log scope and scope.questionId.

HTML

<a class="waves-effect waves-light btn" my-directive="" on-flag="someFunction" question-id="question">Flag</a>

Angular Directive

angular.module('myApp').directive('myDirective', function($http) {
  return {
    restrict: 'A',
    scope: {
      onFlag: '&onFlag',
      questionId: '='
    },
    link: function(scope, element, attrs) {
      element.click(function() {
        console.log(scope);
        console.log(scope.questionId);
        return;
      });
    }
  };
});
9
  • Try logging attrs.onFlag and attrs.questionId Commented Sep 27, 2016 at 18:54
  • i got onFlag() for logging attrs.onFlag and still undefined for attrs.questionId Commented Sep 27, 2016 at 18:57
  • 2
    why you returning element.click function, what you are trying to achieve by doing that? Commented Sep 27, 2016 at 19:00
  • Check out this fiddle: jsfiddle.net/wrnxqvm8 Commented Sep 27, 2016 at 19:01
  • @Jason, where do you define someFunction and question ? Commented Sep 27, 2016 at 19:22

1 Answer 1

3

Try this

elem.bind:- It uses the JQLite which is lite version of JQuery. Here we are writing the code to handle the click event performed on the directive. It is same like JQuery $("class or id").click(). (I hope this explains is sufficient)

angular.module('myApp').directive('myDirective', function($http) {
  return {
    restrict: 'A',
    scope: {
      onFlag: '&onFlag',
      questionId: '='
    },
    link: function(scope, elem, attrs) {
     		elem.bind('click', function() {
        	console.log(scope.questionId);
      });
    }
  };
});

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.