Category Archives: Angular 2 Blog

AngularJs Generate Random Number


We can use JavaScript Math function Math.random() to generate random number in AngularJs. This function returns random number between 0, 1. You can multiply by number to get random number between specified limit. Here in this tutorial, we are going to explain how to use native JavaScript Math.random() number to get the random number.


Works in Angular Version – v1, v2.

AngularJs Generate Random Number Example

You can use Math.random() function simply as below to get random number between 0, 1000.-

Example:

var randNumber = Math.random() * 1000;

Multiply by number such as 1000, 10,000 to generate random number between the specified limit.

Output of above example will be something like this-

1289 // random number between 0, 1000

Angular2 no provider for http


Angular2 no provider for http– Sometimes while working with http service in angular 2 we face issue related to http provider in Angular 2. You need to import HttpModule before using, if it is not imported properly then it can create an issue. Here in this solution we are going to explain the possible reason and fix it.


Angular2 no provider for http Error | Quick Fix

Import the HttpModule before using, it can be imported simply as below-

Angular2 no provider for http Error: port the HttpModule

import { HttpModule } from '@angular/http';

@NgModule({
  imports: [
    HttpModule
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

If you have imported the http module as above then it should fix your problem. Hope this solution will work for you.