<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Angular JS Controller &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/angular-js-controller/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Wed, 02 Jun 2021 15:23:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Angular JS Controller &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Web API With Angular JS</title>
		<link>https://www.sibeeshpassion.com/web-api-with-angular-js-3/</link>
					<comments>https://www.sibeeshpassion.com/web-api-with-angular-js-3/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Thu, 11 Feb 2016 00:00:23 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Angular]]></category>
		<category><![CDATA[Web API]]></category>
		<category><![CDATA[Angular JS]]></category>
		<category><![CDATA[Angular JS Controller]]></category>
		<category><![CDATA[Angular JS Module]]></category>
		<category><![CDATA[Angular JS Service]]></category>
		<category><![CDATA[contenteditable]]></category>
		<category><![CDATA[MVC]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=11185</guid>

					<description><![CDATA[[toc] Introduction In this article, we will learn about CRUD operations in Web API using Angular JS. We will use Visual Studio 2015 to create a Web API and performs the operations. In this project, we are going to create a database and table called tbl_Subcribers which actually contains a list of data. Here we will use Angular JS for all of our client side operations. If you are new to Web API, please read how to retrieve the data from database using Web API here. I am going to explain the complete process in a step by step manner. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h2>Introduction</h2>
<p>In this article, we will learn about CRUD operations in Web API using Angular JS. We will use <a href="http://sibeeshpassion.com/tag/visual-studio/" target="_blank" rel="noopener">Visual Studio</a> 2015 to create a Web API and performs the operations. In this project, we are going to create a database and table called <em>tbl_Subcribers</em> which actually contains a list of data. Here we will use Angular JS for all of our client side operations. If you are new to Web API, please read how to retrieve the data from database using Web API <a href="http://sibeeshpassion.com/load-data-from-database-using-web-api/" target="_blank" rel="noopener">here</a>. I am going to explain the complete process in a step by step manner. I hope you will like this.</p>
<h2><strong>Download the source code</strong></h2>
<p>You can always download the source code here: <a href="https://gallery.technet.microsoft.com/Web-API-With-Angular-JS-8f4ac03d" target="_blank" rel="noopener">Web API and Angular JS</a></p>
<h2><strong>Background</strong></h2>
<p>Yesterday I have got a call from one of my follower. He asked me about Web API, I have explained to him what are all the things I know about the Web API. But he was not convinced with the information I shared through the phone. At last, he asked me to write an article about Web API in a simple manner. So I agreed to do the same. Here I am dedicating this article to him. I hope he can find this useful.</p>
<h3><em>What is a Web API?</em></h3>
<p>A Web API is a kind of a framework which makes building HTTP services easier than ever. It can be used almost everywhere including a wide range of clients, mobile devices, browsers etc. It contains normal MVC features like Model, Controller, Actions, Routing etc. Support all HTTP verbs like POST, GET, DELETE, PUT.</p>
<div id="attachment_10874" style="width: 560px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/10/Why-Web-API.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-10874" class="size-full wp-image-10874" src="http://sibeeshpassion.com/wp-content/uploads/2015/10/Why-Web-API.png" alt="Why Web API" width="550" height="377" srcset="/wp-content/uploads/2015/10/Why-Web-API.png 550w, /wp-content/uploads/2015/10/Why-Web-API-300x206.png 300w, /wp-content/uploads/2015/10/Why-Web-API-320x218.png 320w, /wp-content/uploads/2015/10/Why-Web-API-160x110.png 160w, /wp-content/uploads/2015/10/Why-Web-API-400x274.png 400w" sizes="(max-width: 550px) 100vw, 550px" /></a><p id="caption-attachment-10874" class="wp-caption-text">Why Web API</p></div>
<p><em>Image Courtesy : blogs.msdn.com</em></p>
<div id="attachment_10875" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/10/Why-Web-API1-e1446290457816.png"><img decoding="async" aria-describedby="caption-attachment-10875" class="size-full wp-image-10875" src="http://sibeeshpassion.com/wp-content/uploads/2015/10/Why-Web-API1-e1446290457816.png" alt="Why Web API" width="650" height="454" srcset="/wp-content/uploads/2015/10/Why-Web-API1-e1446290457816.png 650w, /wp-content/uploads/2015/10/Why-Web-API1-e1446290457816-300x210.png 300w, /wp-content/uploads/2015/10/Why-Web-API1-e1446290457816-400x279.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-10875" class="wp-caption-text">Why Web API</p></div>
<p><em>Image Courtesy : forums.asp.net</em></p>
<h2><strong>Using the code</strong></h2>
<h3>Creating project</h3>
<p>We will create our project in Visual Studio 2015. To create a project click File-&gt; New-&gt; Project. And select Web API as a template.</p>
<div id="attachment_11189" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Web-API-Template-e1454769881232.png"><img decoding="async" aria-describedby="caption-attachment-11189" class="size-large wp-image-11189" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Web-API-Template-1024x576.png" alt="Web API Template" width="634" height="357" /></a><p id="caption-attachment-11189" class="wp-caption-text">Web API Template</p></div>
<p>Once you have created a new project, your solution explorer will look like this.</p>
<div id="attachment_11186" style="width: 430px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Web-API-With-Angular-JS-Solution-Explorer.png"><img decoding="async" aria-describedby="caption-attachment-11186" class="size-full wp-image-11186" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Web-API-With-Angular-JS-Solution-Explorer.png" alt="Web API With Angular JS Solution Explorer " width="420" height="672" srcset="/wp-content/uploads/2016/02/Web-API-With-Angular-JS-Solution-Explorer.png 420w, /wp-content/uploads/2016/02/Web-API-With-Angular-JS-Solution-Explorer-188x300.png 188w, /wp-content/uploads/2016/02/Web-API-With-Angular-JS-Solution-Explorer-400x640.png 400w, /wp-content/uploads/2016/02/Web-API-With-Angular-JS-Solution-Explorer-375x600.png 375w" sizes="(max-width: 420px) 100vw, 420px" /></a><p id="caption-attachment-11186" class="wp-caption-text">Web API With Angular JS Solution Explorer</p></div>
<p>As I said, we are going to use Angular JS for all of our client side operations. So the next thing we need to do is, installing the Angular JS from NuGet packages.</p>
<div id="attachment_11188" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Installing-Angular-JS-e1454769674992.png"><img decoding="async" aria-describedby="caption-attachment-11188" class="size-large wp-image-11188" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Installing-Angular-JS-1024x576.png" alt="Installing Angular JS" width="634" height="357" /></a><p id="caption-attachment-11188" class="wp-caption-text">Installing Angular JS</p></div>
<h3><strong>Install Angular JS</strong></h3>
<p>Now we will create a control in our project.</p>
<div id="attachment_10171" style="width: 662px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/CRUD_in_MVC_Using_Web_API_Adding_Control.png"><img decoding="async" aria-describedby="caption-attachment-10171" class="size-full wp-image-10171" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/CRUD_in_MVC_Using_Web_API_Adding_Control.png" alt="CRUD_in_MVC_Using_Web_API_Adding_Control" width="652" height="596" srcset="/wp-content/uploads/2015/08/CRUD_in_MVC_Using_Web_API_Adding_Control.png 652w, /wp-content/uploads/2015/08/CRUD_in_MVC_Using_Web_API_Adding_Control-300x274.png 300w, /wp-content/uploads/2015/08/CRUD_in_MVC_Using_Web_API_Adding_Control-400x366.png 400w" sizes="(max-width: 652px) 100vw, 652px" /></a><p id="caption-attachment-10171" class="wp-caption-text">CRUD_in_MVC_Using_Web_API_Adding_Control</p></div>
<p>Now will create a database. Here I am using SQL Server Management Studio With SQL Server Express.</p>
<h3><strong>Create Database</strong></h3>
<p>Below is the query to create a database.</p>
<p>[sql]<br />
USE [master]<br />
GO</p>
<p>/****** Object: Database [sibeeshpassion] Script Date: 06-02-2016 08:18:42 PM ******/<br />
CREATE DATABASE [sibeeshpassion]<br />
CONTAINMENT = NONE<br />
ON PRIMARY<br />
( NAME = N&#8217;sibeeshpassion&#8217;, FILENAME = N&#8217;C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\sibeeshpassion.mdf&#8217; , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )<br />
LOG ON<br />
( NAME = N&#8217;sibeeshpassion_log&#8217;, FILENAME = N&#8217;C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\sibeeshpassion_log.ldf&#8217; , SIZE = 2048KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET COMPATIBILITY_LEVEL = 120<br />
GO</p>
<p>IF (1 = FULLTEXTSERVICEPROPERTY(&#8216;IsFullTextInstalled&#8217;))<br />
begin<br />
EXEC [sibeeshpassion].[dbo].[sp_fulltext_database] @action = &#8216;enable&#8217;<br />
end<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET ANSI_NULL_DEFAULT OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET ANSI_NULLS OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET ANSI_PADDING OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET ANSI_WARNINGS OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET ARITHABORT OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET AUTO_CLOSE OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET AUTO_SHRINK OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET AUTO_UPDATE_STATISTICS ON<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET CURSOR_CLOSE_ON_COMMIT OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET CURSOR_DEFAULT GLOBAL<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET CONCAT_NULL_YIELDS_NULL OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET NUMERIC_ROUNDABORT OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET QUOTED_IDENTIFIER OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET RECURSIVE_TRIGGERS OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET DISABLE_BROKER<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET AUTO_UPDATE_STATISTICS_ASYNC OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET DATE_CORRELATION_OPTIMIZATION OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET TRUSTWORTHY OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET ALLOW_SNAPSHOT_ISOLATION OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET PARAMETERIZATION SIMPLE<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET READ_COMMITTED_SNAPSHOT OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET HONOR_BROKER_PRIORITY OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET RECOVERY SIMPLE<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET MULTI_USER<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET PAGE_VERIFY CHECKSUM<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET DB_CHAINING OFF<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET TARGET_RECOVERY_TIME = 0 SECONDS<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET DELAYED_DURABILITY = DISABLED<br />
GO</p>
<p>ALTER DATABASE [sibeeshpassion] SET READ_WRITE<br />
GO<br />
[/sql]</p>
<p>Now we can create a table and insert few data into it.</p>
<h3><strong>Create table in database</strong></h3>
<p>Below is the query to create table in database.</p>
<p>[sql]<br />
USE [sibeeshpassion]<br />
GO</p>
<p>/****** Object: Table [dbo].[tbl_Subscribers] Script Date: 06-02-2016 08:21:06 PM ******/<br />
SET ANSI_NULLS ON<br />
GO</p>
<p>SET QUOTED_IDENTIFIER ON<br />
GO</p>
<p>CREATE TABLE [dbo].[tbl_Subscribers](<br />
[SubscriberID] [int] NOT NULL,<br />
[MailID] [nvarchar](50) NOT NULL,<br />
[SubscribedDate] [datetime2](7) NOT NULL,<br />
PRIMARY KEY CLUSTERED<br />
(<br />
[SubscriberID] ASC<br />
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />
) ON [PRIMARY]</p>
<p>GO<br />
[/sql]</p>
<h3><strong>Insert data to table</strong></h3>
<p>[sql]<br />
INSERT INTO [dbo].[tbl_Subscribers] ([SubscriberID], [MailID], [SubscribedDate]) VALUES (1, N&#8217;sibikv4u@gmail.com&#8217;, N&#8217;2015-10-30 00:00:00&#8242;)<br />
INSERT INTO [dbo].[tbl_Subscribers] ([SubscriberID], [MailID], [SubscribedDate]) VALUES (2, N&#8217;sibeesh.venu@gmail.com&#8217;, N&#8217;2015-10-29 00:00:00&#8242;)<br />
INSERT INTO [dbo].[tbl_Subscribers] ([SubscriberID], [MailID], [SubscribedDate]) VALUES (3, N&#8217;ajaybhasy@gmail.com&#8217;, N&#8217;2015-10-28 00:00:00&#8242;)<br />
[/sql]</p>
<p>Our database seems to be ready now. Then we can go ahead and create entity in our project.</p>
<h3><strong>Creating entity model</strong></h3>
<p>To create an entity, please follows the steps shows in the below images.</p>
<div id="attachment_11190" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-1-e1454770826847.png"><img decoding="async" aria-describedby="caption-attachment-11190" class="size-large wp-image-11190" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-1-1024x576.png" alt="Creating Entity Model 1" width="634" height="357" /></a><p id="caption-attachment-11190" class="wp-caption-text">Creating Entity Model 1</p></div>
<div id="attachment_11191" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-2.png"><img decoding="async" aria-describedby="caption-attachment-11191" class="size-large wp-image-11191" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-2-1024x576.png" alt="Creating Entity Model 2" width="634" height="357" srcset="/wp-content/uploads/2016/02/Creting-Entity-Model-2-1024x576.png 1024w, /wp-content/uploads/2016/02/Creting-Entity-Model-2-300x169.png 300w, /wp-content/uploads/2016/02/Creting-Entity-Model-2-768x432.png 768w, /wp-content/uploads/2016/02/Creting-Entity-Model-2-400x225.png 400w, /wp-content/uploads/2016/02/Creting-Entity-Model-2-1067x600.png 1067w, /wp-content/uploads/2016/02/Creting-Entity-Model-2.png 1920w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11191" class="wp-caption-text">Creating Entity Model 2</p></div>
<div id="attachment_11192" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-3.png"><img decoding="async" aria-describedby="caption-attachment-11192" class="size-large wp-image-11192" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-3-1024x576.png" alt="Creating Entity Model 3" width="634" height="357" srcset="/wp-content/uploads/2016/02/Creting-Entity-Model-3-1024x576.png 1024w, /wp-content/uploads/2016/02/Creting-Entity-Model-3-300x169.png 300w, /wp-content/uploads/2016/02/Creting-Entity-Model-3-768x432.png 768w, /wp-content/uploads/2016/02/Creting-Entity-Model-3-400x225.png 400w, /wp-content/uploads/2016/02/Creting-Entity-Model-3-1067x600.png 1067w, /wp-content/uploads/2016/02/Creting-Entity-Model-3.png 1920w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11192" class="wp-caption-text">Creating Entity Model 3</p></div>
<div id="attachment_11193" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-4.png"><img decoding="async" aria-describedby="caption-attachment-11193" class="size-large wp-image-11193" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-4-1024x576.png" alt="Creating Entity Model 4" width="634" height="357" srcset="/wp-content/uploads/2016/02/Creting-Entity-Model-4-1024x576.png 1024w, /wp-content/uploads/2016/02/Creting-Entity-Model-4-300x169.png 300w, /wp-content/uploads/2016/02/Creting-Entity-Model-4-768x432.png 768w, /wp-content/uploads/2016/02/Creting-Entity-Model-4-400x225.png 400w, /wp-content/uploads/2016/02/Creting-Entity-Model-4-1067x600.png 1067w, /wp-content/uploads/2016/02/Creting-Entity-Model-4.png 1920w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11193" class="wp-caption-text">Creating Entity Model 4</p></div>
<div id="attachment_11194" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-5.png"><img decoding="async" aria-describedby="caption-attachment-11194" class="size-large wp-image-11194" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-5-1024x576.png" alt="Creating Entity Model 5" width="634" height="357" srcset="/wp-content/uploads/2016/02/Creting-Entity-Model-5-1024x576.png 1024w, /wp-content/uploads/2016/02/Creting-Entity-Model-5-300x169.png 300w, /wp-content/uploads/2016/02/Creting-Entity-Model-5-768x432.png 768w, /wp-content/uploads/2016/02/Creting-Entity-Model-5-400x225.png 400w, /wp-content/uploads/2016/02/Creting-Entity-Model-5-1067x600.png 1067w, /wp-content/uploads/2016/02/Creting-Entity-Model-5.png 1920w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11194" class="wp-caption-text">Creating Entity Model 5</p></div>
<div id="attachment_11195" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-6.png"><img decoding="async" aria-describedby="caption-attachment-11195" class="size-large wp-image-11195" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Creting-Entity-Model-6-1024x576.png" alt="Creating Entity Model 6" width="634" height="357" srcset="/wp-content/uploads/2016/02/Creting-Entity-Model-6-1024x576.png 1024w, /wp-content/uploads/2016/02/Creting-Entity-Model-6-300x169.png 300w, /wp-content/uploads/2016/02/Creting-Entity-Model-6-768x432.png 768w, /wp-content/uploads/2016/02/Creting-Entity-Model-6-400x225.png 400w, /wp-content/uploads/2016/02/Creting-Entity-Model-6-1067x600.png 1067w, /wp-content/uploads/2016/02/Creting-Entity-Model-6.png 1920w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11195" class="wp-caption-text">Creating Entity Model 6</p></div>
<p>Now it is time to create a API controller.</p>
<p>Select <em>Empty API Controller</em> as template.</p>
<div id="attachment_11196" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Web-API-Contoller-With-Actions.png"><img decoding="async" aria-describedby="caption-attachment-11196" class="size-large wp-image-11196" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Web-API-Contoller-With-Actions-1024x713.png" alt="Web API Contoller With Actions" width="634" height="441" srcset="/wp-content/uploads/2016/02/Web-API-Contoller-With-Actions-1024x713.png 1024w, /wp-content/uploads/2016/02/Web-API-Contoller-With-Actions-300x209.png 300w, /wp-content/uploads/2016/02/Web-API-Contoller-With-Actions-768x535.png 768w, /wp-content/uploads/2016/02/Web-API-Contoller-With-Actions-160x110.png 160w, /wp-content/uploads/2016/02/Web-API-Contoller-With-Actions-400x279.png 400w, /wp-content/uploads/2016/02/Web-API-Contoller-With-Actions-862x600.png 862w, /wp-content/uploads/2016/02/Web-API-Contoller-With-Actions.png 1182w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11196" class="wp-caption-text">Web API Contoller With Actions</p></div>
<h3><strong>Read Operation</strong></h3>
<p>Now you can see some actions are already created for you by default. Cool right? Now as of now we will concentrate only in retrieving the data. So please change the method Get as follows.</p>
<p>[csharp]<br />
public IEnumerable&lt;tbl_Subscribers&gt; Get()<br />
{<br />
return myEntity.tbl_Subscribers.AsEnumerable();<br />
}<br />
[/csharp]</p>
<p>Before the please do not forget to create an instance for your entity.</p>
<p>[csharp]<br />
private sibeeshpassionEntities myEntity = new sibeeshpassionEntities();<br />
[/csharp]</p>
<p>And make sure you have added the needed namespaces with the model.</p>
<p>[csharp]<br />
using System.Data.Entity;<br />
using WebAPIAndAngular.Models;<br />
[/csharp]</p>
<p>As you can notice that we have selected Empty API Controller instead of selecting a normal controller. There are few difference between our normal controller and Empty API Controller.</p>
<h3><strong>Controller VS Empty API Controller</strong></h3>
<p>A controller normally render your views. But an API controller returns the data which is already serialized. A controller action returns JSON() by converting the data. You can get rid of this by using API controller.</p>
<p>Find out more: <a href="http://stackoverflow.com/questions/9494966/difference-between-apicontroller-and-controller-in-asp-net-mvc" target="_blank" rel="noopener">Controller VS API Controller </a></p>
<p>Now our API is ready for action. So it is time to create an another controller and a view. Here I am creating a normal controller with Index view.</p>
<p>Once the view is created, we will created three JS files in the script folder.</p>
<div id="attachment_11197" style="width: 366px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Angular-JS-Operation-FIles.png"><img decoding="async" aria-describedby="caption-attachment-11197" class="size-full wp-image-11197" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Angular-JS-Operation-FIles.png" alt="Angular JS Operation FIles" width="356" height="125" srcset="/wp-content/uploads/2016/02/Angular-JS-Operation-FIles.png 356w, /wp-content/uploads/2016/02/Angular-JS-Operation-FIles-300x105.png 300w" sizes="(max-width: 356px) 100vw, 356px" /></a><p id="caption-attachment-11197" class="wp-caption-text">Angular JS Operation FIles</p></div>
<p>Now we will start our Angular JS part. If you are new to Angular JS, I strongly recommend you to check here.<a href="http://sibeeshpassion.com/category/angularjs/" target="_blank" rel="noopener">Angular JS Basics</a></p>
<p>Open the file Module.js and create an app.</p>
<p>[js]<br />
var app;<br />
(function () {<br />
app = angular.module(&#8220;APIModule&#8221;, []);<br />
})();<br />
[/js]</p>
<p>Here APIModule is the name of our module. Check <a href="https://docs.angularjs.org/error/$injector/nomod?p0=APIModule" target="_blank" rel="noopener">here </a>for more information.</p>
<p>Open the file Service.js and create a service as follows.</p>
<p>[js]<br />
app.service(&#8220;APIService&#8221;, function ($http) {<br />
this.getSubs = function () {<br />
return $http.get(&#8220;api/Subscriber&#8221;)<br />
}<br />
});<br />
[/js]</p>
<p>Here APIService is our service name which we will call from our controller. The <em>api/Subscriber</em> will call the Get method in our API controller.</p>
<div id="attachment_11198" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Get-Operation-In-API-Controller-e1454772443988.png"><img decoding="async" aria-describedby="caption-attachment-11198" class="size-full wp-image-11198" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Get-Operation-In-API-Controller-e1454772443988.png" alt="Get Operation In API Controller" width="650" height="162" srcset="/wp-content/uploads/2016/02/Get-Operation-In-API-Controller-e1454772443988.png 650w, /wp-content/uploads/2016/02/Get-Operation-In-API-Controller-e1454772443988-300x75.png 300w, /wp-content/uploads/2016/02/Get-Operation-In-API-Controller-e1454772443988-400x100.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11198" class="wp-caption-text">Get Operation In API Controller</p></div>
<p>Now Open our Controller.JS and write code as below.</p>
<p>[js]<br />
app.controller(&#8216;APIController&#8217;, function ($scope, APIService) {<br />
getAll();<br />
function getAll()<br />
{<br />
var servCall = APIService.getSubs();<br />
servCall.then(function (d) {<br />
$scope.subscriber = d.data;<br />
}, function (error) {<br />
$log.error(&#8216;Oops! Something went wrong while fetching the data.&#8217;)<br />
})<br />
}<br />
})<br />
[/js]</p>
<p>We are calling the getSubs function which we created in our service. Once we get the data we are assigning it to the $scope.subscriber, so that we can use it in our view.</p>
<p>Now the angular JS part for retrieving all data is done. Can we do the needed changes in the view now?</p>
<h3><strong>Updating View</strong></h3>
<p>Open the Index.cshtml view and change it as below.</p>
<p>[html]</p>
<p>@{<br />
ViewBag.Title = &#8220;Welcome&#8221;;<br />
}<br />
&lt;style&gt;<br />
table, tr, td, th {<br />
border: 1px solid #ccc;<br />
padding: 10px;<br />
margin: 10px;<br />
}<br />
&lt;/style&gt;</p>
<p>&lt;h2&gt;Welcome to Sibeesh Passion&#8217;s Email List&lt;/h2&gt;<br />
&lt;body data-ng-app=&#8221;APIModule&#8221;&gt;<br />
&lt;div id=&#8221;tblSubs&#8221; ng-controller=&#8221;APIController&#8221;&gt;<br />
&lt;table&gt;<br />
&lt;tr&gt;<br />
&lt;th&gt;ID&lt;/th&gt;<br />
&lt;th&gt;Email ID&lt;/th&gt;<br />
&lt;th&gt;Subscribed Date&lt;/th&gt;<br />
&lt;/tr&gt;<br />
&lt;tbody data-ng-repeat=&#8221;sub in subscriber&#8221;&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;{{sub.SubscriberID}}&lt;/td&gt;<br />
&lt;td&gt;{{sub.MailID}}&lt;/td&gt;<br />
&lt;td&gt;{{sub.SubscribedDate}}&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/tbody&gt;<br />
&lt;/table&gt;<br />
&lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;script src=&#8221;~/Scripts/angular.js&#8221;&gt;&lt;/script&gt;<br />
&lt;script src=&#8221;~/Scripts/angular-route.js&#8221;&gt;&lt;/script&gt;<br />
&lt;script src=&#8221;~/Scripts/APIScripts/Module.js&#8221;&gt;&lt;/script&gt;<br />
&lt;script src=&#8221;~/Scripts/APIScripts/Service.js&#8221;&gt;&lt;/script&gt;<br />
&lt;script src=&#8221;~/Scripts/APIScripts/Controller.js&#8221;&gt;&lt;/script&gt;<br />
[/html]</p>
<p>Please don&#8217;r forget to load the needed scripts. Here we have set body as our data-ng-app and table as our ng-controller. We are looping through the data using data-ng-repeat.</p>
<p>If everything is done, we can build the application and see the output.</p>
<div id="attachment_11199" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Wep-API-Get-All-Record.png"><img decoding="async" aria-describedby="caption-attachment-11199" class="size-large wp-image-11199" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Wep-API-Get-All-Record-1024x576.png" alt="Wep API Get All Record" width="634" height="357" srcset="/wp-content/uploads/2016/02/Wep-API-Get-All-Record-1024x576.png 1024w, /wp-content/uploads/2016/02/Wep-API-Get-All-Record-300x169.png 300w, /wp-content/uploads/2016/02/Wep-API-Get-All-Record-768x432.png 768w, /wp-content/uploads/2016/02/Wep-API-Get-All-Record-400x225.png 400w, /wp-content/uploads/2016/02/Wep-API-Get-All-Record-1067x600.png 1067w, /wp-content/uploads/2016/02/Wep-API-Get-All-Record.png 1344w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11199" class="wp-caption-text">Wep API Get All Record</p></div>
<p>So far our READ operation is done. Now We will move into CREATE part.</p>
<h3><strong>Create Operation</strong></h3>
<p>First we will concentrate on the view part as of now. Just add the below codes to your view.</p>
<p>[html]<br />
&lt;div class=&#8221;form-group&#8221;&gt;<br />
&lt;label for=&#8221;email&#8221;&gt;Sbscribe here&lt;/label&gt;<br />
&lt;input type=&#8221;email&#8221; class=&#8221;form-control&#8221; id=&#8221;email&#8221; placeholder=&#8221;Enter email&#8221; [required=&#8221;string&#8221; ] data-ng-model=&#8221;mailid&#8221; /&gt;<br />
&lt;/div&gt;<br />
&lt;button type=&#8221;button&#8221; class=&#8221;btn btn-default&#8221; data-ng-click=&#8221;saveSubs();&#8221;&gt;Submit&lt;/button&gt;<br />
[/html]</p>
<p>This will give you an output as follows.</p>
<div id="attachment_11209" style="width: 441px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/View-Design-For-Create.png"><img decoding="async" aria-describedby="caption-attachment-11209" class="size-full wp-image-11209" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/View-Design-For-Create.png" alt="View Design For Create" width="431" height="154" srcset="/wp-content/uploads/2016/02/View-Design-For-Create.png 431w, /wp-content/uploads/2016/02/View-Design-For-Create-300x107.png 300w, /wp-content/uploads/2016/02/View-Design-For-Create-400x143.png 400w" sizes="(max-width: 431px) 100vw, 431px" /></a><p id="caption-attachment-11209" class="wp-caption-text">View Design For Create</p></div>
<p>As you can see, we are firing the function <em>saveSubs()</em> in <em>data-ng-click</em>. So let us see what we need to write in it.</p>
<p>In the Controller.js you need to create a function as below.</p>
<p>[js]<br />
$scope.saveSubs = function () {<br />
var sub = {<br />
MailID: $scope.mailid,<br />
SubscribedDate: new Date()<br />
};<br />
var saveSubs = APIService.saveSubscriber(sub);<br />
saveSubs.then(function (d) {<br />
getAll();<br />
}, function (error) {<br />
console.log(&#8216;Oops! Something went wrong while saving the data.&#8217;)<br />
})<br />
};<br />
[/js]</p>
<p>Did you see that we are calling an another function which is in our APIService. So now we need to create a function <em>saveSubscriber</em> in Service.js. Shall we?</p>
<p>[js]<br />
this.saveSubscriber = function (sub) {<br />
return $http({<br />
method: &#8216;post&#8217;,<br />
data: sub,<br />
url: &#8216;api/Subscriber&#8217;<br />
});<br />
}<br />
[/js]</p>
<p>So all set, the rest is to create a function in our API Controller.</p>
<p>[csharp]<br />
// POST: api/Subscriber<br />
public void Post(tbl_Subscribers sub)<br />
{<br />
if (ModelState.IsValid)<br />
{<br />
myEntity.tbl_Subscribers.Add(sub);<br />
myEntity.SaveChanges();<br />
}<br />
}<br />
[/csharp]</p>
<p>That&#8217;s cool, now you will be able to create data through our API with the help of Angular JS. Now shall we move into UPDATE operation.</p>
<h3><strong>Update Operation</strong></h3>
<p>Before going to the code part we will do some changes in our table design. We are going to make one field (Mail ID field) editable when ever user double clicks in it. And of course we will update the edited data to the database when ever user leaves that field. Sounds cool? Now please change the view as follows.</p>
<p>[html]<br />
&lt;div id=&#8221;tblSubs&#8221; ng-controller=&#8221;APIController&#8221;&gt;<br />
&lt;table&gt;<br />
&lt;tr&gt;<br />
&lt;th&gt;ID&lt;/th&gt;<br />
&lt;th&gt;Email ID ( Double click to update)&lt;/th&gt;<br />
&lt;th&gt;Subscribed Date&lt;/th&gt;<br />
&lt;th&gt;&lt;/th&gt;<br />
&lt;/tr&gt;<br />
&lt;tbody data-ng-repeat=&#8221;sub in subscriber&#8221;&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;{{sub.SubscriberID}}&lt;/td&gt;<br />
&lt;td ng-blur=&#8221;updSubscriber(sub,$event)&#8221; ng-dblclick=&#8221;makeEditable($event)&#8221;&gt;{{sub.MailID}}&lt;/td&gt;<br />
&lt;td&gt;{{sub.SubscribedDate}}&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/tbody&gt;<br />
&lt;/table&gt;<br />
&lt;div class=&#8221;form-group&#8221;&gt;<br />
&lt;label for=&#8221;email&#8221;&gt;Sbscribe here&lt;/label&gt;<br />
&lt;input type=&#8221;email&#8221; class=&#8221;form-control&#8221; id=&#8221;email&#8221; placeholder=&#8221;Enter email&#8221; [required=&#8221;string&#8221; ] data-ng-model=&#8221;mailid&#8221; /&gt;<br />
&lt;/div&gt;<br />
&lt;button type=&#8221;button&#8221; class=&#8221;btn btn-default&#8221; data-ng-click=&#8221;saveSubs();&#8221;&gt;Submit&lt;/button&gt;<br />
&lt;/div&gt;<br />
[/html]</p>
<p>The below is the main change we did.</p>
<p>[html]<br />
&lt;td ng-blur=&#8221;updSubscriber(sub,$event)&#8221; ng-dblclick=&#8221;makeEditable($event)&#8221;&gt;{{sub.MailID}}&lt;/td&gt;<br />
[/html]</p>
<p>In <em>ng-blur</em> we are calling the function <em>updSubscriber</em> with parameter $event and the current subscriber details. And in <em>ng-dblclick</em> we are calling the function <em>makeEditable</em> with parameter $event which actually holds the UI details and current events.</p>
<p>Below is the code for the function <em>makeEditable</em> in Controller.js</p>
<p>[js]<br />
$scope.makeEditable = function (obj) {<br />
obj.target.setAttribute(&#8220;contenteditable&#8221;, true);<br />
obj.target.focus();<br />
};<br />
[/js]</p>
<p>As you can see we are setting the attribute <em>contenteditable</em> to true using <em>setAttribute</em> function. Now we will look into the function <em>updSubscriber</em>.</p>
<p>Add a function in Controller.js</p>
<p>[js]<br />
$scope.updSubscriber = function (sub, eve) {<br />
sub.MailID = eve.currentTarget.innerText;<br />
var upd = APIService.updateSubscriber(sub);<br />
upd.then(function (d) {<br />
getAll();<br />
}, function (error) {<br />
console.log(&#8216;Oops! Something went wrong while updating the data.&#8217;)<br />
})<br />
};<br />
[/js]</p>
<p>Add a relative function in Service.js</p>
<p>[js]<br />
this.updateSubscriber = function (sub) {<br />
return $http({<br />
method: &#8216;put&#8217;,<br />
data: sub,<br />
url: &#8216;api/Subscriber&#8217;<br />
});<br />
}<br />
[/js]</p>
<p>Now we need to add a function in our Web API controller.</p>
<p>[csharp]<br />
// PUT: api/Subscriber/5<br />
public void Put(tbl_Subscribers sub)<br />
{<br />
if (ModelState.IsValid)<br />
{<br />
myEntity.Entry(sub).State = EntityState.Modified;<br />
try<br />
{<br />
myEntity.SaveChanges();<br />
}<br />
catch (Exception)<br />
{<br />
throw;<br />
}<br />
}<br />
}<br />
[/csharp]</p>
<p>Now you will be able to update your record. What is pending now? Yes, DELETE operation.</p>
<h3><strong>Delete Operation</strong></h3>
<p>Make some changes in the view as follows.</p>
<p>[html]<br />
&lt;tbody data-ng-repeat=&#8221;sub in subscriber&#8221;&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;{{sub.SubscriberID}}&lt;/td&gt;<br />
&lt;td ng-blur=&#8221;updSubscriber(sub,$event)&#8221; ng-dblclick=&#8221;makeEditable($event)&#8221;&gt;{{sub.MailID}}&lt;/td&gt;<br />
&lt;td&gt;{{sub.SubscribedDate}}&lt;/td&gt;<br />
&lt;td&gt;<br />
&lt;input type=&#8221;button&#8221; id=&#8221;Delete&#8221; value=&#8221;Delete&#8221; data-ng-click=&#8221;dltSubscriber(sub.SubscriberID)&#8221; /&gt;<br />
&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/tbody&gt;<br />
[/html]</p>
<p>Now add the new function in Controller.js</p>
<p>[js]<br />
$scope.dltSubscriber = function (subID) {<br />
var dlt = APIService.deleteSubscriber(subID);<br />
dlt.then(function (d) {<br />
getAll();<br />
}, function (error) {<br />
console.log(&#8216;Oops! Something went wrong while deleting the data.&#8217;)<br />
})<br />
};<br />
[/js]</p>
<p>Create a service in Service.js now.</p>
<p>[js]<br />
this.deleteSubscriber = function (subID) {<br />
var url = &#8216;api/Subscriber/&#8217; + subID;<br />
return $http({<br />
method: &#8216;delete&#8217;,<br />
data: subID,<br />
url: url<br />
});<br />
}<br />
[/js]</p>
<p>Now it is time to create our delete method in Web API controller.</p>
<p>[csharp]<br />
// DELETE: api/Subscriber/5<br />
public void Delete(int id)<br />
{<br />
tbl_Subscribers dlt = myEntity.tbl_Subscribers.Find(id);<br />
if (dlt != null)<br />
{<br />
try<br />
{<br />
myEntity.tbl_Subscribers.Remove(dlt);<br />
myEntity.SaveChanges();<br />
}<br />
catch (Exception)<br />
{<br />
throw;<br />
}<br />
}<br />
}<br />
[/csharp]</p>
<p>That is all. We did it. Now build your application and see an output as follows.</p>
<div id="attachment_11211" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Web-API-With-Angular-JS1-e1454928109942.png"><img decoding="async" aria-describedby="caption-attachment-11211" class="size-full wp-image-11211" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Web-API-With-Angular-JS1-e1454928109942.png" alt="Web API With Angular JS" width="650" height="522" srcset="/wp-content/uploads/2016/02/Web-API-With-Angular-JS1-e1454928109942.png 650w, /wp-content/uploads/2016/02/Web-API-With-Angular-JS1-e1454928109942-300x241.png 300w, /wp-content/uploads/2016/02/Web-API-With-Angular-JS1-e1454928109942-400x321.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11211" class="wp-caption-text">Web API With Angular JS</p></div>
<p>Have a happy coding.</p>
<h2><strong>Conclusion</strong></h2>
<p>Did I miss anything that you may think which is needed? Did you try Web API yet? Have you ever wanted to do this requirement? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h2><strong>Your turn. What do you think?</strong></h2>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/web-api-with-angular-js-3/feed/</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
			</item>
	</channel>
</rss>
