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.
bower install mathjs --save
Reference mathjs in your main html page
<script src="path/to/bower_components/mathjs/dist/math.min.js" type="text/javascript"></script>
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
As time passed, I'm adding this updated response.
If you're using Angular, chances are that you're using npm as well. To add mathjs, you can follow the official procedure
npm install mathjs
npm install @types/mathjs
The two above commands will update the package.json and package-lock.json as needed.
Then, as described in the Getting Started, you need to import separately each of the mathjs commands you want to use. So, for example, to perform square root in a typescript class:
import { sqrt } from 'mathjs';
export class MyClass {
function calculate(n: number): number {
return sqrt(n);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With