0

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!

2
  • 1
    I don't understand the "may not exist" part, can you explain in a little more detail? The directive is used in multiple contexts and sometimes the enclosing controller has this function and sometimes not? Commented May 6, 2014 at 4:25
  • This might help you: stackoverflow.com/questions/16598337/… Commented May 6, 2014 at 4:59

1 Answer 1

1

Just call your function from a function like this :

scope.$apply(function(){
   fn(one, two);
});
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.