how to pass ASP.NET MVC view values to angular js controller - angularjs

I want to pass ASP.NET MVC view (.cshtml) values to angular js controller. I am familiar with Angular js, But not on MVC. I have values in MVC cshtml. I want to pass that value to my angular controller. Please provide me some info or demo project or link which explains in detail.
i want somthing like below,
Get value from mvc model and pass it to cshtml.
from cshtml pass value to angular js controller and display in angular html page
I do not want to use cshtml as my view. I want to get data from cshtml to angular controller and display in seperate html

Using the very first tutorial I pulled up I grabbed this snippet:
var AwesomeAngularMVCApp = angular.module('AwesomeAngularMVCApp', ['ngRoute']);
AwesomeAngularMVCApp.controller('HomeController', HomeController);
var configFunction = function ($routeProvider) {
$routeProvider.
when('/Listings', {
url: 'routesDemo/one'
})
.when('/Listing', {
url: 'routesDemo/two'
})
.when('/Listings', {
url: 'routesDemo/three'
});
}
configFunction.$inject = ['$routeProvider'];
AwesomeAngularMVCApp.config(configFunction);
Now this how you link a view to a controller action in MVC:
using System.Web.Mvc;
namespace AwesomeAngularMVCApp.Controllers
{
public class RoutesDemoController : Controller
{
public ActionResult One(string title)
{
var listings = db.Articles.Contain(title);
return PartialView(listings, "..\Views\Shared\WhateverPartialView.cshtml");
}
[HttpPost]
public async Task<ActionResult> Two(Article article)
{
if(ModelState.isValid){
_db.Add(article)
}
return View(article); //This one returns entire page
}
public JsonResult Three(string title)
{
var listing = db.Articles.Where(t => t.Title == title).SingleOrDefault();
return Json(listing, JsonRequestBehavior.AllowGet);
}
}
}
These partial views would be in the Views folder in a sub folder RoutesDemo
one.cshtml GET
two.cshtml POST ie: a href="#/Article/6" type=submit" class="btn"
$("form").submit( function(){
$.ajax( function(url, data){
});
});
three.cshtml GET ie
$.ajax({
url: '#Url.Action("routeDemo", "three")',
//url: baseUrl + url,
data: {
search: searchBlue.val()
},
success: function (data) {
$("#msg").html("Results for" + searchBlue.val());
searchBlue.searchMeme({ searchComplete: true });
$('#main').fadeOut(800, function () {
$('#main').html("" + data + "").fadeIn().delay(800);
});
searchBlue.val("");
},
error: function (xhr, status, error) {
alert(error);
}
});
If you want to pass values to the angular controller use razor syntax and bind the value to the html input. The top of the view file will make it so angular knows what type of object to expect.
IE: Top of Two.cshtml
#model AwesomeAngularMVCApp.Article
That should be it besides route.config for angular
If you want to handle the Model object using razor syntax check out this tutorial. Pretty cut and dry.If that is not enough I will show example of binding #Model.attribute/property to an html element after work tomorrow
EDIT
<div class="row-fluid" ng-controller="PersonDetailsController" ng-init="personId=#Model.Id">
Angular injects it in the scope during initialization, so you can refer to it as $scope.personId

Related

How to show JSON data in HTML

I want to show the following JSON in an Angular page :
{"_id":"58b11","name":"Somename","city":"Paris","number":456789123,"__v":0}
I get this data by clicking on a link in an Angular page:
<td ng-click="getData(user._id)">
{{user.name}}
</td>
I am able to get the data from the db and show in the angular page in proper HTML. Now when I click on the link, i get the desired data in JSON on the page http://localhost:8080/api/students/58b11. I want to be able to use Angular {{}} to show the data in proper HTML format.
Angular :
//Created Student factory like this:
app.factory('Student', function($resource) {
var data = $resource('/api/students/:id', { id: '#_id' }, {
update: {
method: 'PUT'
}
});
return data;
});
//controller:
app.controller("dummy2", function($scope, $http, Student, $window){
$scope.getData = function(userID){
$window.location.assign("/api/students/"+userID);
}
)};
NodeJs:
app.use('/api', require('./routes/api'));
Thanks.

Passing the URL/route parameter from laravel and using in angular

I am new to Laravel 5 and angular.
I am using Laravel routing for traversal and backend operations and angular for just UI operations like fetching data and binding UI grid, etc.
I have following route defined in routes.php file below
routes.php
Route::pattern('clientid', '[0-9]+');
//used for AJAX call from angularjs and populating ui-grid
Route::get('getclients/{clientid?}', 'ClientController#getClients');
//used for displaying Laravel view with ui-grid
Route::get('client/{clientid?}', 'ClientController#showClients');
Please find the angular files:
app.js
var appClients = angular.module('getclients', ['clientsService', 'ui.grid', 'ui.grid.exporter', 'ui.grid.selection']);
clientController.js
appClients.controller('ClientsController', ['$scope', '$http', 'Client', '$interval', '$q', function ($scope, $http, Client, $interval, $q) {
/* Defining UI grid options*/
.
.
/* Calling service to fill the grid*/
Client.get(clientid)
.success(function (data, status, headers, config) {
if (data.length > 0) {
$scope.gridOptions.data = data;
}
});
}
clientsService.js
angular.module('clientsService', [])
.service('Client', function ($http) {
return {
// Get all the photos
get: function (clientid) {
if (clientid !== '') {
return $http.get('/myproject/public/getclients/' + clientid);
}
else {
return $http.get('/myproject/public/getclients/');
}
}
}
});
/*
**Note:**
Have already defined route in routes.php for using the same above:
Route::get('getclients/{clientid?}', 'ClientController#getClients');
*/
EXAMPLE:
Step 1:
Say I am hitting URL: http://<domain>/public/myproject/client/2
The following route would catch it and redirect to view where the ui-grid is present
Route::get('client/{clientid?}', 'ClientController#showClients');
Step 2:
Now, somehow need to figure out how to pass that **2** to angular so that I could pass that parameter while making ajax call and get grid data
I am confused as to how we could use the the url parameter from Laravel in angular?
I reckon that I am missing some concept or doing something wrong here.
Could anyone help me out?
Just a workaround to make it work with angular and without jquery.
From routes.php, the control is transferred to showClients action in ClientsController.php
ClientsController.php (Laravel Controller):
Passed the variable to Laravel view from controller using following statement:
public function showClients($clientid = '') {
return view('masters.clients', compact('clientid'));
}
Clients.php (Laravel View)
Added clientidmodel as ng-model and initialized it with passed clientid from Laravel controller using ng-init
<div ng-app="">
<div ng-controller="ClientsController">
<input type="text" name="txtClientId" ng-model="clientidmodel" style="display: none;" ng-init="clientidmodel = '{!!$clientid!!}'"/>
</div>
</div>
clientController.js
Added the watch to the angular model so that we can capture the initial value passed.
$scope.$watch("clientidmodel", function () {
Client.get($scope.clientidmodel)
.success(function (data, status, headers, config) {
if (data.length > 0) {
$scope.gridOptions.data = data;
}
});
});
Not sure whether this is the efficient way but as of now got the things working with this workaround.
Please let me know in case of any better way to approach the same.
You can achieve this in jquery by
var pathname = window.location.href;
var lastItem = pathname.split("/").pop(-1);
Note : Here you will get the last element
i.e.,
If your url is like yourapp.com/app#/product/15 then the script will return 15. That's the last element after / . You can change this according to your wish.
Then you can pass the value directly inside your Laravel Controller.
$.ajax({
type: "POST",
dataType: 'text',
crossOrigin : true,
data: {param : lastItem},
url: serviceUrl+'/getReceipeDetails',
})
.done(function( data ) {
var result = jQuery.parseJSON(data);
if(result.success==1)
{
$(".yourresult").html('Controller return success');
}
else
{
$(".yourresult").html('Controller return failure');
}
})
.fail( function(xhr, textStatus, errorThrown) {
console.log(errorThrown);
});

implementing external links to a page with angular-scroll and ng-route

So this is is an angularjs app.
I have implemented this angular-scroll api :https://github.com/oblador/angular-scroll, to show a catalog of products, where the content is loaded from db. this catalog has all the subcategories (with its products) and every subcategory has an anchor identified like: anchor+categoryId.
So from the menu , i click a category and it scroll nicely to the correct section.
The problem arise when I need to create some links from other pages of the site, to go to an specific section category inside the catalog. Because I have ng-route, i need to create a new url to redirect to the catalog, and there capture when the content is loaded to do the scroll to the required category.
BUT I have a directive associated with the route of the catalog, that looks for the partials depending on the domain of the client, so to show the correct template i have to use an $http , get the content and replace it in my directive.
Because that I dont know how i can know when the content of the directive is ready to make the call to the scroll... better show some code here :
this is the route that is receiving the call
$routeProvider.
when('/products/category/:categoryId/page/:page/anchor/:anchorId?', {
template:'<product-display-view></product-display-view>',
controller: 'ProductListCtrl',
access: {
authorizedRoles: [USER_ROLES.all]
},
resolve: {
wait : 'waitForIt',
prefetchDataProducts: ['waitForIt','$route','SearchService',
function(waitForIt,$route,SearchService) {
return waitForIt.then(function() {
return SearchService.getProducts($route.current.params.categoryId,$route.current.params.page);
});
}],
prefetchDataCategories:['waitForIt','CategoryService',
function(waitForIt,CategoryService) {
return waitForIt.then(function() {
return CategoryService.getCategories();
});
}]
}
}).
this is the directive product-display
productDirectives.directive('productDisplayView',['$rootScope','$compile','$http','$templateCache' ,'$document',
function($rootScope,$compile, $http, $templateCache,$document){
return {
restrict: 'E',
link: function (scope, element, attrs) {
var templateUrl = 'users/catwizardAngularCore/app/partials/themes/' + scope.app.theme.themeName + '/partials/product-display.html';
$http.get(templateUrl, {cache: $templateCache})
.success(function (templateContent) {
element.replaceWith($compile(templateContent)(scope));
});
/* this doesn't work because the someElement doesn't exist*/
var newHash = 'anchor' + scope.anchorId;
var someElement = angular.element(document.getElementById(newHash));
angular.element(someElement).ready(function () {
$document.scrollToElement(someElement, 200, 2000);
});
}
}]);
There is a duplicate question with the correct answer, but it has not been accepted yet so I am copying the answer here.
The $anchorScroll has to occur after the page has been rendered,
otherwise the anchor doesn't exist. This can be achieved using
$timeout().
$timeout(function() {
$anchorScroll('myAnchor');
});
Credits to Tony

$http.get method not working on ng-submit

I want $http.get method to work when a form is submitted.
Here is my code. The object $scope.questions is being set when the method is called but the data doesn't show up in the div. Moreover, when the $http.get method is outside the signIn() function it works just fine.
$scope.signIn = function(data) {
$location.path('/profile');
var url = "database/fetch_data.php?query=";
var query = "Select * from question where userId=2";
url += query;
$http.get(url).success(function(questionData) {
$scope.questions = questionData;
console.log($scope.questions);
});
};
<div>
User Profile
<br/>Question Posted
<br/>
<input ng-model="query.title" id="value" type="text" placeholder="Search by Title..." ">
<div>
<ul>
<li ng-repeat="question in questions | filter: query ">
{{question.title}}
</li>
</ul>
</div>
<br/>
</div>
You need to move your $location.path('/profile') inside your http request. Remember that a http request is async call. You should redirect after getting the data not before.
$scope.signIn = function(data) {
var url = "database/fetch_data.php?query=";
var query = "Select * from question where userId=2";
url += query;
$http.get(url).success(function(questionData) {
$scope.questions = questionData;
console.log($scope.questions);
$location.path('/profile');
});
};
If you're redirecting to another route with a completely separate scope you will lose any scope you're setting in the success handling.
From what I'm reading you're clicking a button to do an action. After that action you're redirecting to another page with a separate controller and trying to persist the data.
Unfortunately, Angular hasn't figured out a great way to do this. The easiest way to persist data through controllers and scope is to create a service that will store it in one controller and grab it in another controller.
For instance:
$scope.signIn = function(data) {
var url = "database/fetch_data.php?query=";
var query = "Select * from question where userId=2";
url += query;
$http.get(url).success(function(questionData) {
$location.path('/profile');
storageService.store("question", questiondata)
});
};
Your new factory to persist data through:
angular.module('moduleName').factory('storageService', [
function () {
return {
store: function (key, value) {
localStorage.setItem(key, JSON.stringify(value));
},
get: function(key) {
return JSON.parse(localStorage.getItem(key));
},
remove: function(key) {
localStorage.removeItem(key);
}
}
}
]);
Other controller to access data:
$scope.question = storageService.get("question");
// remove localstorage after you've grabbed it in the new controller
storageService.remove("question");
An alternative to doing the somewhat 'hacky' way of using localStorage to persist data through controllers is to use ui-router and have a resolve on the route you're redirecting to.
For instance:
$scope.signIn = function(data) {
$state.go('profile');
};
In your route file:
.state('profile', {
url: '/profile'
controller: profileControllerName,
templateUrl: 'profileHtmlTemplate.html',
resolve: {
'questions': [function() {
var url = "database/fetch_data.php?query=";
var query = "Select * from question where userId=2";
url += query;
$http.get(url).success(function(res) {
return res.data;
});
}]
}
}
In your profile controller:
Inject your 'questions' resolve into your controller and assign `$scope.question = questions;
This will make the HTTP call as soon as you click the route, return the data if successful, then render the page. It will NOT render the page if the resolve does not return success. This will ensure your data will be loaded before you load the page that depends on that data.
I would highly recommend using services to hold your HTTP calls for specific parts of your application. If you have a GET questions, POST question, PUT question. I would create a questionService and make all my HTTP methods there so you don't have to clutter your routes. You would only have to call:
.state('profile', {
url: '/profile'
controller: profileControllerName,
templateUrl: 'profileHtmlTemplate.html',
resolve: {
'questions': [function() {
return questionService.getQuestions(id).then(function(res) {
return res.data;
})
}]
}
}

Storing null value inside database while trying to save data using angularjs and asp.net mvc

I'm trying to create a single page application using ASP.net MVC and angular.js. I am following the instruction Here. My model class is as follows
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
and my app.js class is as follows
//Step-01 : Define Module
var PersonModule = angular.module('PersonModule', ['ngRoute', 'ngResource']);
//Step:04 : Define Module Configuration
PersonModule.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/', {
controller: DetailsController,
templateUrl: 'list.html'
})
.when('/new', {
controller: CreateController,
templateUrl: 'new.html'
})
.otherwise({ retdirectTo: '/' });
}]);
//Step : 02 Define Factory for Hold resource and request server
PersonModule.factory('PersonResource', function ($resource) {
return $resource('/api/Person/:id', { id: '#id' }, { update: { method: 'PUT', isArrary: true } });
});
//Step : 03 Define Controller i.e. business logic
var CreateController = function ($scope, $location, PersonResource) {
//Create a variable in the controller for save button catpion
$scope.Action = 'Create';
//Define method for Create
$scope.Create = function () {
//Call the Save method of $resource and passed paremeter to it $scope.Person here Person is a model
PersonResource.save({ post: $scope.Person }, function () {
//If call is success then call path method of $location directive for redirect
$location.path('#/');
});
}
}
var DetailsController = function ($scope, PersonResource) {
//Call the query method of $resource directive
$scope.Persons = PersonResource.query();
}
and my html file code is as follows
<h2>{{Action}} Person</h2>
<form>
<div>
<label for="Person.Name">Person:</label>
<input ng-model="Person.Name" name="Name" />
</div>
<div>
<label for="Person.Address">Address:</label>
<input ng-model="Person.Address" name="Address" />
</div>
<div>
Cancel
<button ng-click="Create()">{{Action}}</button>
</div>
</form>
Below given my Server Side Controller Put method:
Here is my Server Controller Action Method that put data:
public HttpResponseMessage Post(Person person)
{
context.Persons.Add(person);
context.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, person);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = person.Id }));
return response;
}
when i press save button it get null value for Name and Address and though Id is an identity field it get value for Id and finally save to database with null value at Name and address.
when I check developer resource from Network tab of Chrome it shows me following information:
The structure of the json send from client should map to structure of the .net class for binding to work correctly.
Try to send the post\put as
PersonResource.save($scope.Person, function (){
Also your Person json object should have a id property that would be used to construct the resource url.
Also the PUT resource declaration should only declare isArray=true if the response is an array.

Resources