0

I have the following return of an directive in angularJS

return {
            restrict: _restrict,
            link: function (scope, element, attrs) {
                $timeout(LinkPre, 0);  //Calling a scoped method
            },
            templateUrl: ConstrutorapiTemplatesChart,
            scope: "@",
            controller: Controller
        }

My LinkPre function is the function below

var LinkPre = function (scope, elem, attrs) {
            attrs.$observe(_attrUrl, function (relatorio) {
                if (relatorio != "") AoMudarUrl(scope, elem, relatorio);
            });
        }

I got an error on the LinkPre function, that the attrs is undefined

Cannot read property '$observe' of undefined

Anyone has any ideia why?

Obs: I need that the Link function be executed after the directive is rendered

CodeWarrior
  • 2,721
  • 3
  • 18
  • 18
Patrick Sampaio
  • 390
  • 2
  • 13

2 Answers2

0

Firstly you need to pass correctly your parameters to the function. Then if your function is defined inside the associated controller, you need to add the controller in the link function and call the function through the controller reference.

Try with:

link: function (scope, element, attrs, myController) {
    $timeout(myController.LinkPre.bind(this, scope, element, attrs));  
}
quirimmo
  • 9,800
  • 3
  • 30
  • 45
0

thats because your LinkPre function doesnt recive any aguments when called by timeout, and thats why attrs and all of the parameters will be undefined, you could try the following:

$timeout(function() {
    LinkPre(scope, elem, attrs);
});

no need for the end zero either

Strife86
  • 1,135
  • 1
  • 10
  • 18