Ich bin neu zu eckig und versuchen zu verstehen, wie Service, Komponente und Controller funktioniert.Wie Winkelkomponente, Service und Controller funktioniert
Lassen Sie uns sagen, ich habe die folgenden:
Datei Service: products.service.js
'use strict';
angular.module('myApp.products')
.factory('ProductService', function() {
var products = [
{_id: 1, title: 'Product 1', price: 123.45, quantity:10, description:'Some description 1'},
{_id: 2, title: 'Product 2', price: 123.45, quantity:10, description:'Some description 2'},
{_id: 3, title: 'Product 3', price: 123.45, quantity:10, description:'Some description 3'},
{_id: 4, title: 'Product 4', price: 123.45, quantity:10, description:'Some description 4'},
{_id: 5, title: 'Product 5', price: 123.45, quantity:10, description:'Some description 5'}
];
return {
alllProducts: function() {
return products;
}
};
});
Controller-Datei: products.controller.js
'use strict';
(function(){
class ProductsComponent {
constructor() {
// how should i connect them here like below?
// this.products = ProductService.allProducts();
}
}
angular.module('myApp')
.component('products', {
templateUrl: 'app/products/products.html',
controller: ProductsComponent
});
})();
ich bin mit Generator- eckig-vollstapelig. Wie kann ich alle Produkte im Controller-Konstruktor verbinden und in der Vorlage anzeigen?
Dank
danke, das macht viel mehr Sinn für mich. Wenn ich auf stateParams zugreifen muss, sollte ich es injizieren wie: Konstruktor (ProductService, $ stateParams)? – triston
Ja genau, solange es im Modul System registriert ist, können Sie es in Controller/Komponente/Richtlinie/Dienste etc Deklarationen injizieren. – Dan