angularjsmathjs

How to inject mathjs into Angular.js?


How can I inject mathjs into Angular.js?

I can't get it to work - I used bower install mathjs --save to install it.

Update: The JS min file is properly injected into index.html

Do I need to inject it into the main module or into the controller? I tried both and got a white screen on page load with an error message about the non-availability of the module.


Solution

  • Step 1

    bower install mathjs --save
    

    Step 2

    Reference mathjs in your main html page

    <script src="path/to/bower_components/mathjs/dist/math.min.js" type="text/javascript"></script>
    

    Step 3

    This dependency is not angular-aware, there is no module to inject as a dependency of your angular application. As mathjs exposes a global math object, you can use it in an angular component as follows :

    math.round(0.123, 2) // gives back 0.12