Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you check if & variable is set in directive

In a directive, where you set a method callback through an arg, say:

<my-directive callback='showAlert()' />

You can retrieve the variable through the scope setting in the return:

scope: {
    callback: "&callback"
}

If the callback is not set, eg:

<my-directive />

The value of $scope.callback is still:

$scope.callback():function (locals) {
    return parentGet(scope, locals);
}

Is there a good way to check that the callback was not set?

like image 274
stevedbrown Avatar asked Feb 17 '14 03:02

stevedbrown


2 Answers

One way i can think off is to check the attribute parameter for the directive name like

link: function (scope, elm, attrs) {

   if(attrs.callback) {
       //this attribute has been defined.
   }

}
like image 93
Chandermani Avatar answered Sep 18 '22 12:09

Chandermani


You can use &? insteand of &. It will make $scope.callback equal to undefined if it is not provided.

like image 37
Lyubimov Roman Avatar answered Sep 20 '22 12:09

Lyubimov Roman