0

For me AngularJS directives looks like wrapped functionality, like some web component. I am trying to wrap progress bar from Bootstrap i.e.:

Bootstrap progress bar

I made basic directive thats fine, but I would like to add some API to that component. Some functions which I can use in controller that could control component behaviour. E.g. start, stop, reset, gotopercent etc. I made that functions at controller but I think it is not best practice since that functions should belong to directive, not a controller so I can easily reuse it in other pages.

Question:

How to create functions in AngularJS directive which allows me to control component behavior.

1 Answer 1

1

I made that functions at controller but I think it is not best practice since that functions should belong to directive, not a controller so I can easily reuse it in other pages.


Well, no. Functions do belong into controllers - but directives can have their own controller where you define your "directive API" independent from you ApplicationController or PageController.

Take a look at the angular-ui/bootstrap repo, where there's already a directive for the bootstrap progressbar.

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.