1

I am trying to pass an optional parameter isvalid from my html to the directive. I have followed all the steps mentioned in documentation but it still looks like i am doing something wrong.. i am not able to read the value in my directive. can you let me know what am i doing wrong?

HTML

 <customvideo isvalid="true"></div>

MY Directive
Update: I had simplified for questioning purpose and hence you were seeing $scope. I have updated the actual directive now

    // Set the directive
    angular
        .module('custom.directives')
        .directive('customvideo', customvideoDirective);

    // Set the directive $injections
    customvideoDirective.$inject = ['$Scope'];


 function customvideoDirective($Scope)  
{
            return {
                compile: compile,
                restrict: 'A',
                $Scope: {
                    isvalid: '=?'
                }
            };

  function compile() {
      console.log($Scope.isvalid);  //this is undefined 
     }
  }

})();
3
  • $Scope should be scope Commented Sep 28, 2017 at 18:29
  • thankyou @PankajParkar. i inject it as $scope. i removed it here for simplicity... it is something like this : // Set the directive angular .module('custom.directives') .directive('customvideo', customvideoDirective); // Set the directive $injections customvideoDirective.$inject = [ '$Scope' ]; Commented Sep 28, 2017 at 18:33
  • Please check my answer below.. Commented Sep 28, 2017 at 18:34

2 Answers 2

1

Change you $Scope property inside Directive Definition Object (DDO) should be changed scope. Also use link function instead of using compile, as compile function has access to only raw DOM, there will be no scope available in it. Even you can return and PreLink/PostLink from compile function, but in this case I believe using link would be appropriate.

angular.module('directives')
.directive('customvideo', function () {
    return {
       link: link,
       restrict: 'A',
       scope: {
          isvalid: '=?'
       }
    }
);

function link(scope) {
   console.log(scope.isvalid);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Found a way to work it out.. not sure if it is the best.. but hey.. it works :) Posting it here for those who come here for answers... I was writing the syntax for compile wrongly... I changed it to

 function  compile(tElement, tAttrs, transclude)
  {
    // the tAttrs param has all the attributes associated with the element
    console.log(tAttrs.isvalid);
  }

2 Comments

This is not correct way of doing it. Compile function is not correct place to look for value passed to directive. please follow above answer..
Thanks Pankaj.. Would would mind explaining why not compile vs link in my scenario.. i dont want to modify my dom or anything.. i just want to read the value .. I thought Link should be used when I actually want to modify the Dom. Would that be a wrong assumption?

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.