In this blog,we will learn to create a simple angularjs application using Dotnet Framework.So Let's start to implement it step by step.
1.Open Visual Studio and Create a New MVC project.
![](http://findnerd.com/imagedata/5634/5634.png)
2.Now create a MVC5 Empty Controller as I have created a studentController.
![](http://findnerd.s3.amazonaws.com/imagedata/5630/5630.png)
Note:adding a controller will automatically create a folder with the same name in the Views folder.So user will get all the corresponding views of newly added controller in that folder.
3.We will create an action method in the controller.
public class StudentController : Controller
{
// GET: Student
public ActionResult Index()
{
return View();
}
}
4.Now Right click on the action method Index and add a view with the available layout in the shared folder.
![](http://findnerd.s3.amazonaws.com/imagedata/5629/5629.png)
5.Now To implement Angualrjs in asp.net mvc, we will add angularjs library thorugh Nuget Package Manager.
![](http://findnerd.s3.amazonaws.com/imagedata/5631/5631.png)
Then Search Angularjs and install it,as in my demo only angular core features are required so I have installed angularjs core.
![](http://findnerd.s3.amazonaws.com/imagedata/5627/5627.png)
When AngularJs will be installed then you will get some angularjs files in your scripts folder.
![](http://findnerd.s3.amazonaws.com/imagedata/5632/5632.png)
6.Now you have to provide the reference of angularjs in your application.
So In App_Start folder,Open Bundle.config and include angularjs script in bundle.
bundles.Add(new ScriptBundle("~/bundles/angular").Include(
"~/Scripts/angular.js",
"~/Scripts/angular-mocks.js"));
7.Now add this bundle in _Layout.cshtml,so that Views which will use this layout they will automatically able to implement angularjs. It will be added after bootstrap and before render.section.
@Scripts.Render("~/bundles/angular")
8.Now go to the view Index and write your html using AngularJs
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<div ng-app>
<p>{{"Hello" + " " + "Angularjs"}}</p>
<br />
<p>Enter Your name:<input type="text" ng-model="name" style="border:ridge" /> </p>
<p>Hello <span ng-bind="name"></span></p>
</div>
In this view ng-app,ng-model and ng-bind are the directives of angularjs.
Ng-app:this directive will define angularjs and link your application to html.
ng-model: this directive binds the value with html input controls as we can see we have used input type text.
ng-bind:this directive binds the value with html tag as we are using paragraph tag.
Now Debug your application and see the result:Type your name in input field and see the result.
![](http://findnerd.s3.amazonaws.com/imagedata/5628/5628.png)
0 Comment(s)