Skip to content

Instantly share code, notes, and snippets.

@TiagoSoczek
Last active February 24, 2017 19:59
Show Gist options
  • Select an option

  • Save TiagoSoczek/13550e046d8b6ddba63a433cf922421e to your computer and use it in GitHub Desktop.

Select an option

Save TiagoSoczek/13550e046d8b6ddba63a433cf922421e to your computer and use it in GitHub Desktop.
Contoso Shop - SPA
// definindo módulo raiz
angular.module('app', []);
angular.module('app').
controller('createProductController', createProductController);
angular.module('app').
controller('listProductsController', listProductsController);
var apiBaseAddress = 'http://localhost:57666';
function listProductsController($http)
{
var vm = this;
vm.products = [];
vm.load = load;
function load()
{
$http.get(apiBaseAddress + '/products').then(showData);
}
function showData(response)
{
console.log(response.data);
vm.products = response.data;
}
}
function createProductController($http)
{
var vm = this;
vm.form = {};
vm.save = save;
function save()
{
// TODO: Criar combo de seleção
vm.form.departamentId = 1;
$http.post(apiBaseAddress + '/products', vm.form).
then(showSuccess, showError);
}
function showError(response)
{
console.error(response);
window.alert('Falha ao processar! Ver console para detalhes.');
}
function showSuccess()
{
vm.form = {};
window.alert('Sucesso!');
}
}
@model ListProductsViewModel
@{
ViewBag.Title = "Produtos";
}
<div class="panel panel-primary">
<div class="panel-heading">Lista de Produtos</div>
<div class="panel-body">
<form class="form-inline" asp-controller="Products" asp-action="Index"
method="post">
<div class="form-group">
@*@Html.LabelFor(x => x.Keywords)
@Html.TextBoxFor(x => x.Keywords, new { @class = "form-control" })*@
<label asp-for="Keywords"></label>
<input asp-for="Keywords" class="form-control">
</div>
<button type="submit" class="btn btn-default">OK</button>
</form>
</div>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Id</th>
<th>Nome</th>
<th>Preco</th>
<th>Data Criacao</th>
</tr>
</thead>
@foreach (var product in Model.Products)
{
<tr>
<td>@product.Id</td>
<td>@product.Name</td>
<td>@product.Price</td>
<td>@product.CreatedAt</td>
</tr>
}
</table>
</div>
@section Scripts
{
<script>
alert('Opa');
</script>
}
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<title>Contoso Shop</title>
<link rel="stylesheet" href="lib/bootstrap/dist/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<h1>Contoso Shop</h1>
<div class="panel panel-primary" ng-controller="createProductController as vm">
<div class="panel-heading">Criar Produto</div>
<div class="panel-body">
<form ng-submit="vm.save()">
<div class="form-group">
<label for="name">Nome</label>
<input type="text" class="form-control" id="name" ng-model="vm.form.name">
</div>
<div class="form-group">
<label for="description">Descrição</label>
<input type="text" class="form-control" id="description" ng-model="vm.form.description">
</div>
<div class="form-group">
<label for="price">Preço</label>
<input type="text" class="form-control" id="price" ng-model="vm.form.price">
</div>
<button type="submit" class="btn btn-default">Salvar</button>
</form>
</div>
</div>
<div class="panel panel-primary" ng-controller="listProductsController as vm">
<div class="panel-heading">Lista de Produtos</div>
<div class="panel-body">
<a class="btn btn-success" ng-click="vm.load()">Atualizar</a>
</div>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Id</th>
<th>Nome</th>
<th>Preco</th>
</tr>
</thead>
<tr ng-repeat="product in vm.products">
<td ng-bind="product.id"></td>
<td ng-bind="product.name"></td>
<td ng-bind="product.price"></td>
</tr>
</table>
</div>
</div>
<script src="lib/angular/angular.min.js"></script>
<script src="lib/angular-route/angular-route.min.js"></script>
<script src="app/app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment