This post will work off a project I created in a previous post – Creating Your First ASP.NET Core 1.0 Application from Scratch – and it will get you set up with AngularJS, Grunt, and Bower.
Add an NPM Configuration File and install Grunt and Bower
1. Right click the project and select Add > New Item. Select NPM Configuration File and name it package.json
2. Open up package.json and add the following 4 dependencies
What did we just do?
In our package.json file, we’ve indicated that we need three NPM packages named grunt, grunt-contrib-uglify, and grunt-contrib-watch. After the name of each package, we’ve specified the version of the package that we need. Grunt and Bower are now installed in our project!
Add a Grunt Configuration File
1. Right click the project and select Add > New Item. Select Grunt Configuration File and name it gruntfile.js
What is Grunt?
2. Open up gruntfile.js and load your plugins by adding the following.
3. Next add your configurations to the grunt file
'wwwroot/app.js': ['Scripts/app.js', 'Scripts/**/*.js']
grunt.registerTask('default', ['uglify', 'watch']);
4. Your gruntfile.js should now look like this
What did we just do?
Add JQuery and Bootstrap to our project using Bower
What is Bower?
Bower is a package manager created by Twitter that is designed to support front-end development. You can use Bower to manage resources such as AngularJS, jQuery, and Bootstrap.
1. Right click the project and select Add > New Item. Select Bower Configuration File and name it bower.json
2. Open up bower.json and add JQuery and Bootstrap to its dependencies
3. Your bower.json file and projectfolder structure should now look like this:
Wow, now we have Nuget, NPM, and Bower package managers in our project. Why do we need all of them and what do they do? Check out my blog post about that here – Nuget vs NPM vs Bower.
1. Open up bower.json and add these AngularJS modules to its dependencies. For this project we will just be using the “angular package”, but I wanted to show here that “angular-resource” and “angular-route” are added in the same way.
2. Save and compile the project and you will see that the above modules were added to you wwwroot folder and your Bower folder. Should look like this:
1. Right click the project and select Add > New Folder. Name it “Scripts”.
3. This is the same app.js file that we look for in our gruntfile.js code a few steps above. The next thing we will do is initialize the angular application. To do this, add the following to app.js
var app = angular.module("myApp", );
4. To have Grunt uglify app.js and add it to our wwwroot folter, do the following. Open up Visual Studio’s Task Runner Explorer (View > Other Windows > Task Runner Explorer)
5. Refresh the Task Runner Explorer. You should see your Gruntfile.js there. Drill down into Alias Tasks. Right click and run default. This will minify (uglify) your app.js file and add it to your wwwroot.
Use static files
1. You want to tell your app to use static files like app.js and the other angular and bootstrap files. To do this open up Startup.cs and add this to the Configure(IApplicationBuilder app) method.
2. The method Configure(IApplicationBuilder app) should now look like this:
Writing some AngularJS!
1. Open up Views/Home/Index.cshtml
2. Replace what is in Index.cshtml with the following
<meta name="viewport" content="width =device-width" />
<title>AngularJS and ASP.NET 5 Example</title>
@*add styles here*@
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<h2>Hello Home Index Page</h2>
Name : <input type="text" ng-model="name">
What did we just do?
In the above html we are loading Bootstrap, JQuery, AngularJS, and app.js. We are then adding ng-app=”myApp” to the HTML tag. The ng-app directive defines an AngularJS application and its module is created in app.js. If these angular terms are unfamiliar to you, please check out http://www.w3schools.com/angular/angular_intro.asp for a more detailed explanation of the Angular code seen here.
You now have a working ASP.NET Core 1 AngularJS Application.
Check it out on GitHub