0

Directive:

app.directive('myDialog', ["$timeout", function($timeout) {
  var fade = "fade";

  return {
    template: '<div ng-controller="AccountCtrl" ng-cloak="">' +
              '<div class="ui {{message.type}} message message-overwrite" ng-class="{'+ fade +': enabled} ">' +
              '<div class="header">{{message.title}}</div>' +
              '<p>{{message.content}}</p>' +
              '</div>' +
              '</div>',
    link: $timeout(function($scope) {
      $scope.enabled = true;
      console.log("Test");
    }, 1000)
  };
}]);

When trying to run my directive, I keep getting the error:

angular.js:13550 TypeError: Cannot set property 'enabled' of undefined

But I'm not sure what the proper place to define 'enabled' is.

0

1 Answer 1

1

Link function of a directive cannot be set to a $timeout if you want to access the scope. Angular invokes the link function and passes in scope, element and attrs in that order. So, when timeout is used, the inner function, which in your case thinks will be called by angular is not called. $timeout calls that. Not the directive. And that's why it's undefined.

What you could do to fix that is:

link: function($scope) {
    $timeout(function(){
        $scope.enabled = true;
        console.log("Test");
    }, 1000);
}

So, move the timeout inside the link function. And of course you need to inject $timeout into the directive, which you already have.

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.