dzm
dzm

Reputation: 23534

Angularjs calling scope function in a directive

I'm trying to call a scope function from within a controller (which may or may not exist) from a directive. Calling fn() by itself, doesn't work because the fn() may not exist.

I've used scope.$apply('fn') which works, however I need to pass in parameters into the function, such as scope.$apply('fn(one, two)'); - this does not work.

Any suggestions how to call a function with parameters in a directive, that doesn't break if the controller has not yet defined the function?

Thanks!

Upvotes: 0

Views: 116

Answers (1)

L105
L105

Reputation: 5419

Just call your function from a function like this :

scope.$apply(function(){
   fn(one, two);
});

Upvotes: 1

Related Questions