简体   繁体   中英

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!

Just call your function from a function like this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM