I am creating a recipe app, I have different categories that I want to dynamically load on click. To pull recipes I have a url (http get request) which I want to extend with a string depending on what category i click on. I understand that this can be done with parameters but I do not understand how this works as I am new to angular and Ionic.
The base url would be
http://api.yummly.com/v1/api/recipes?_app_id=/////&_app_key=/////
On the end I would want to add either one of these.
396^Dairy-Free
393^Gluten-Free
391^Nut-Free
393^Wheat-Free and so on. Could someone please give me an idea on how to implement this
My HTML currently is
<ion-item [navPush] = "listingPage" detail-push>
<img src="http://placehold.it/500x500">
<h1>Glueten Free</h1>
</ion-item>
<ion-item>
<img src="http://placehold.it/500x500">
<h1>Category 2</h1>
</ion-item>
<ion-item>
<img src="http://placehold.it/500x500">
<h1>Category 3</h1>
</ion-item>
<ion-item>
<img src="http://placehold.it/500x500">
<h1>Category 4</h1>
</ion-item>
and HTTP request is
this.http.get('http://api.yummly.com/v1/api/recipes?_app_id=////&_app_key=//////')
.map(res => res.json())
.subscribe(data => {
// we've got back the raw data, now generate the core schedule data
// and save the data for later reference
console.log(data);
this.listing = data.matches;
resolve(this.listing);
});
});
Currently I only have 1 url loading on a page, but I want this to change depending on what category selected. Many thanks
You have to pass the category id when the user will select the category and then call the function and pass the category id to that function. In that function, call the http method and you can use the category id in the URL.
<button click="getData(this.value)"></button>
In ts.file
getData(cat_id : any){
......
you can use cat_id here in URL and call http method
......
}
Related
I'm a on learning stage, and a little bit of a noob, sorry if this is a stupid question. I'm working on a Movie DB project, with movies from live API, Favorite list, JWT auth, etc. I'm now on the end part, have everything done so far, and have one problem. Every person who click's on see details about movie, the movie will be sent on my DB. And with this user can save movie to his favorite table (.net Crud operations). Everything work but when I want to get movies from a table that's between movies table and users table (favorite table many to many relation) I get a error in my swagger everything is working, and I can get every movie that's in favorite, but in angular I user *ngFor, and the error say that it must return a array, but I'm getting a Object, every other get request works only this one doesn't. Here is my code and error:
You can see I get everything I want in console, but UI is Error
Component .ts and HTML:
//HTML
<div *ngFor="let fav of requests" class="col-xl-3 col-lg-4 col-md-6 col-sm-6 col-xs-12">
<div class="card">
<img src="{{fav.Poster}}" class="card-img-top">
<div class="card-body">
<p class="card-text"> {{fav.Title}}</p>
</div>
</div>
</div>
.Ts
export class FavoriteListComponent implements OnInit {
constructor(public service: MovieService) { }
requests: any = [];
ngOnInit(): void {
this.getFav();
}
getFav(){
this.service.getFavorite1().subscribe(
res => {
this.requests = res;
console.log(res)
});
}
}
MovieService
getFavorite1(){
return this.http.get('http://localhost:5002/api/Favorite/getUsersFavorite').pipe(map(res => res));
}
And this is my get request in .Net 5
[Authorize]
[HttpGet("getUsersFavorite")]
public async Task<IActionResult> GetUsersFavoriteMovies()
{
string userId = User.Claims.First(a => a.Type == "UserID").Value;
var user = await _context.DbUsers.Where(n => n.Id == Int32.Parse(userId)).Select(movies => new FavoriteView()
{
ImdbId = movies.Favorites.Select(n => n.ImdbId).ToList(),
Title = movies.Favorites.Select(n => n.Movies.Title).ToList(),
Poster = movies.Favorites.Select(n => n.Movies.Poster).ToList()
}).FirstOrDefaultAsync();
return Ok(user);
}
I tried a lot of thing, the closest I get is this, and when I change in .net request the .FirstOrDefaultAsync() to .ToListAsync(). When I it to ToListAsync I get this:
In this case I don't get any error but nothing is showing in UI
I know the question is big, but I'm desperate, I'm stuck on this a few day's, and every help is great. Thank you!!!
Use this way, it will work.
<ng-container *ngIf="requests">
<div
*ngFor="let fav of requests.imdbId; index as i"
class="col-xl-3 col-lg-4 col-md-6 col-sm-6 col-xs-12"
>
<div class="card">
<img [src]="requests.poster[i]" class="card-img-top" />
<div class="card-body">
<p class="card-text">{{ requests.title[i] }}</p>
</div>
</div>
</div>
</ng-container>
Example:
https://stackblitz.com/edit/angular-ivy-f251ko?file=src%2Fapp%2Fapp.component.html
I have two buttons which are encapsulated with forms at SpringMVC/Thymeleaf.The interesting thing is I am not if I remove upper button, when I click second button nothing happens. I meant I am not able to call the PostMaping inside Controller which can be called if first button there. It could be an interesting issue for you but I tested it many times and could not find a reason why that happens.Could you please look at the code and let me know how it happens?
First Button:(encapsulated with form)
<form class="needs-validation" action="/showlimit" th:action="#{/showlimit}" th:object="${internetCustomer}" method="post">
<button type="submit"
class="list-group-item list-group-item-action list-group-item-primary">
<th:block th:text="#{label.customerdetails.button.limits}">
First Button
</th:block>
</button>
Second Button:(Encapsulated with form to call PostMapping at Controller)
<form class="needs-validation" action="/showlimit/{customerId}(customerId='123')}" th:action="#{/showlimit}" th:object="${limitModel}" method="post">
<button type="submit"
class="list-group-item list-group-item-action list-group-item- primary">
<th:block th:text="#{label.customerdetails.button.limits}">
Second Button
</th:block>
</button>
When you submit your second form, you are making a POST request to the url /showlimit/123. Since you are passing a parameter in an URL to show something, this should be a GET mapping, not a POST one. This affects how your request is processed by the controller, since POST request parameters are expected in the body, not the URL.
To pass this parameter to the controller, use the following mapping and method:
#GetMapping(/showlimit/{customerId})
public String foo(#PathVariable String customerId, Model model){
...
}
And change the method in your form to GET.
I have an ASP .Net core MVC 2.0 app where I implemented a shopping cart for an e-commerce site as outlined in this video series. In the app, user can enter search queries for items which are displayed in a table. Each item in the table can be clicked on to display another page that has additional details for that item as well as a add to cart button.
The site features for searching, displaying the item results, and the details page are all implemented as actions in one controller class while the shopping cart functionality is all implemented in a separate controller class.
For some reason, when I click on the "Add to Cart" button for an item, a url of the form http://<hostname>/<controllerName>/<controllerACtion>/<id>is requested, but the corresponding controller action isn't called. This is strange because I'm using essentially the same code to call the action for adding to shopping cart as I did for displaying details. For this reason, I think the issue is related to the setup of the routes.
Here is the route setup in startup.cs:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=resortDeals}/{action=Index}/{id?}");
routes.MapRoute(
name: "shoppingcart",
template: "{controller=ShoppingCartController}/{action=AddToShopingCart}/{id?}");
});
Here is the shopping cart controller structure:
public class ShoppingCartController : Controller
{
public ViewResult Index()
{
...
}
public RedirectToActionResult AddToShopingCart(int dealId)
{
...
}
}
Below is the code for how the action is being called once the add to cart button is clicked:
<div class="addToCart text-right">
<p class="button ">
<a class="btn btn-success" id="cartButton" asp-controller="ShoppingCartController" asp-action="AddToShoppingCart" asp-route-id="#Model.RecNo">
Add to cart
</a>
</p>
</div>
What could be preventing the controller action from being called once the button is clicked?
I had an issue after publishing on the production server, when clicking on the button there was no response and I had to submit the button using Javascript.
<div class="addToCart text-right">
<p class="button ">
<button class="btn btn-success" onclick="search(#Model.RecNo)">
Add to cart
</button>
</p>
</div>
<script>
function addToCart(recNo) {
document.getElementById('cartForm').action = "/ShoppingCart/AddToShoppingCart/" + recNo;
document.getElementById('cartForm').submit();
}
</script>
I don't think you need the following route because the default route will handle the shoppingcart routes as well.
template: "{controller=ShoppingCartController}/{action=AddToShopingCart}/{id?}");
Also, try
<a class="btn btn-success" id="cartButton" asp-controller="ShoppingCart" asp-action="AddToShoppingCart" asp-route-id="#Model.RecNo">
I have removed the Controller word from asp-controller attribute
Also, change the input parameter name of AddToShopingCart method to id instead of dealId
public RedirectToActionResult AddToShopingCart(int id)
{
...
}
Im trying to open a single record from my #each loop of items into its own view by clicking a link that says 'see more', which will take me to the single article. I set up my Flow router and its working but i cannot see the data that's supposed to come in.
the template helper for the single article looks like this
Template.collectionSingle.helpers({
articles: function () {
var id = FlowRouter.getParam('_id')
return theCollection.findOne({_id: id});
}
});
}
my route looks like this
FlowRouter.route('/collection/:_id', {
name: 'collection',
action() {
BlazeLayout.render("AppLayout", {main: "collectionSingle"});
}
});
and the template "collectionSingle" looks like this
<template name="collectionSingle">
<h1>{{title}}</h1>
<h1>This is a test</h1>
<img src="{{thumbnail}}" alt="" />
</template>
when i navigate to http://localhost:3000/collection/thePublication-0
all i see is the test message 'This is a test', but i don't see the {{title}} nor the thumbnail.
furthermore, when i change:
return theCollection.findOne({_id: id});
to another one of my collections :
return OtherCollection.findOne({_id: id});
http://localhost:3000/collection/thePublication-0
remains the same.
how can i successfully have a single articles page for each of my articles and have them linked properly with flow router?
You need to actually use your template helper that is returning the data context:
<template name="collectionSingle">
<h1>{{title}}</h1>
<h1>This is a test</h1>
{{#with articles}}
<img src="{{thumbnail}}" alt="" />
{{/with}}
</template>
Since your articles helper returns a single document you use {{#with articles}}. If it returned a cursor or array you would loop over that with {{#each articles}}. The normal convention is to use the singular form for the former, the plural for the latter.
I need to display the shopping cart outside of the the HeaderLinks partial which takes the cart items out of the model passed by the CommonController.HeaderLinks action. By creating a new controller with an action that passes the cart info I was able to get the custom ui element from our template working. Right now the controller is inside a custom plugin I got going for some other stuff. The way I see it I have two options:
Leave the controller in the plugin project and live with the fact that if the plugin for some reason is not installed (i.e. a fresh checkout from a new dev.) the theme is going to break, possibly redirecting the user to the error view.
Put the controller in the Controllers folder at Nop.Web with the downside that this would add an extra step to the process of upgrading NopCommerce. If similar issues arise this could get ugly pretty quick.
So my question is: where is the best place to put the controller? Or is there a simpler way to do this thing with the shopping cart?
Regards,
Jose
i will show you a shortcut way to solve it. First create a partial (razor) view e.g _CustomPartialView and then add these usings at the top inside the partial view
#using Nop.Core;
#using Nop.Core.Infrastructure;
#using Nop.Services.Orders;
then next is to query the shopping cart which is easy becuase nopCommerce has already static way of accessing depency resolver method i.e EngineContext.Current.Resolve<T>(). In your case (for shopping cart) it could be the following;
#{
var shoppingCartEnabled = EngineContext.Current.Resolve<Nop.Services.Security.IPermissionService>()
.Authorize(Nop.Services.Security.StandardPermissionProvider.EnableShoppingCart);
var customer = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer;
int shoppingCartItems = 0;
if (customer.HasShoppingCartItems)
{
shoppingCartItems = customer.ShoppingCartItems
.Where(sci => sci.ShoppingCartType == Nop.Core.Domain.Orders.ShoppingCartType.ShoppingCart)
.LimitPerStore(EngineContext.Current.Resolve<IStoreContext>().CurrentStore.Id)
.ToList()
.GetTotalProducts();
}
if (shoppingCartEnabled)
{
<div class="header-right pull-right wrap-cart hidden-xs ">
<div class="cart-top pull-right">
<div id="cart">
<span class="icon fa fa-shopping-cart"></span>
<div class="heading">
<a href="#Url.RouteUrl("ShoppingCart")" class="ico-cart dropdown-toggle visible-md visible-lg" data-toggle="dropdown" data-hover="dropdown">
<h4 class="cart-label">
#T("ShoppingCart")
<span>#T("ShoppingCart.HeaderQuantity", shoppingCartItems)</span>
</h4>
</a>
</div>
<div class="content">
#if (!String.IsNullOrWhiteSpace(Html.Action("FlyoutShoppingCart", "ShoppingCart").ToString()))
{
<li>#Html.Action("FlyoutShoppingCart", "ShoppingCart")</li>
}
</div>
</div>
</div>
</div>
}
}
Let me know if you need more help :)