Answer the question
In order to leave comments, you need to log in
Working with forms in angularjs (add to list, remove, list search)?
Good day to all!
there is this topic:
<!DOCTYPE html>
<html ng-app='store'>
<head>
<title>NG</title>
</head>
<body>
<div id='products' ng-controller="ProductController as products">
<button type="button" ng-click='products.toggleProducts()'>Products</button>
<p>Number of products: {{products.number}}</p>
<form name='addProduct' ng-submit='products.addProduct()'>
<input type="plain/text" placeholder='Enter name' ng-model='products.new_product_name'/>
<input type="plain/text" placeholder='Enter price' ng-model='products.new_product_price' />
<input type="submit" value='Add' />
</form>
<div id='products-list'>
<div ng-repeat='product in products.products'>
<ul>
<li>{{ product.name }}: {{ product.price | currency }}</li>
</ul>
</div>
<ul>
<li>{{ products.new_product_name }}: {{ products.new_product_price | currency }}</li>
</ul>
</div>
</div>
<div id='customers' ng-controller="CustomerController as customers">
<button type="button" ng-click='customers.toggleCustomers()'>Customers</button>
<p>Number of customers: {{customers.number}}</p>
<div id='customers-list'>
<div ng-repeat='customer in customers.customers'>
<ul>
<li>Customer: {{ customer.name | uppercase }}, city: {{ customer.city }},
age: {{ customer.age }} - <img ng-src='{{ customer.ava }}' /></li>
</ul>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="app.js"></script>
</body>
</html>
(function() {
var app = angular.module('store', []);
app.controller("ProductController", function() {
this.products = [
{name: '1', price: 2.0},
{name: '2', price: 2.5},
{name: '3', price: 5.8}
];
this.number = this.products.length;
this.toggleProducts = function() {
$('#products-list').slideToggle();
};
this.addProduct = function(????????) {
this.products.push(???????);
};
});
app.controller("CustomerController", function() {
this.customers = [
{name: 'Bob', city: 'Kiev', age: 40, ava: 'bob.jpg'},
{name: 'Sue', city: 'Kiev', age: 30, ava: 'sue.jpg'},
{name: 'Dan', city: 'Kiev', age: 25, ava: 'dan.jpg'}
];
this.number = customers.length;
this.toggleCustomers = function() {
$('#customers-list').slideToggle();
};
});
})();
Answer the question
In order to leave comments, you need to log in
Can it be like this
<input type="plain/text" placeholder='Enter name' ng-model='products.new.name'/>
<input type="plain/text" placeholder='Enter price' ng-model='products.new.price' />
this.addProduct = function() {
this.products.push(this.new);
this.new = {price:'', name:''};
};
<ul>
<li>
{{ products.new_product_name }}: {{ products.new_product_price | currency }}
<span ng-click="products.remove($index)">удалить</span>
</li>
</ul>
this.remove = function($index){
this.products.splice($index, 1);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question