Today, I faced a requirement where I need to implement file save functionality in an Angular 4 application. In this quick post, I will show you how to implement this functionality in Angular 4 using FileSaver.js module.
Angular 4 is a good choice for building modern web applications. It comes bundled with Test utilities that makes it easy to write good quality test cases quite easily. Angular official testing guide does a good job explaining how to get started with testing Angular components. One thing that it does not cover is how to test components that use two way data binding using
ngModel directive. Last week I had to write test case for a component that uses data binding so I end up learning a lot about testing such components. In this blog, I will share my learnings with you.
I use Angular 4 at work for building a single page application. Today, I faced the problem where I had to use different base paths for router and static assets. Angular makes use of the base href to tell router how to compose navigation URLs. If your application exists at the root, then you can use
/ as the
href value as shown below.
I wanted to use
/users as my application base for router and
/public as base for my assets.
If you build the project the Angular project for the production environment using the default values you will get the following
$ ng build -prod
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>SampleNgApp</title> <base href="/"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"> <link href="styles.xxxx.bundle.css" rel="stylesheet"/></head> <body> <app-root>Loading...</app-root> </body> </html>
This generated page will not work as it router will use
/ as base for composing navigation URLs and static assets will be looked in the current directory.
To solve my problem, I used
—-deploy-url options as shown below.
$ ng build -prod --base-href /users --deploy-url /public
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>SampleNgApp</title> <base href="/users"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"> <link href="/public/styles.xxxx.bundle.css" rel="stylesheet"/></head> <body> <app-root>Loading...</app-root> </body> </html>
This way I solved my problem.
Today, I decided use Teradata Covalent components in my Angular 4 application. After adding the dependency, my project stopped compiling. I started getting following errors
ERROR in /Users/dev/app//node_modules/@covalent/core/common/animations/toggle/toggle.directive.d.ts (2,10): Module "/Users/dev/app/node_modules/@angular/animations/animations" has no exported member 'AnimationBuilder'. ERROR in /Users/dev/app/node_modules/@covalent/core/common/animations/fade/fade.directive.d.ts (2,10): Module "/Users/dev/app/node_modules/@angular/animations/animations:" has no exported member 'AnimationBuilder'.
I tried googling this error but couldn’t find a fix. Then, i looked at the Covalent Github repository to find the Angular version they are using. It turned out to be Angular version issue. I was using Angular 4.1.3 and Covalent needs Angular version 4.2.0 and above. After making the change, I was able to get my app working.