【发布时间】:2016-09-02 03:08:34
【问题描述】:
我是 Angular 新手,并试图了解服务、组件和控制器的工作原理。
假设我有以下几点:
服务文件: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;
}
};
});
控制器文件: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
});
})();
我正在使用 generator-angular-fullstack。如何连接控制器构造函数中的所有产品并在模板中显示它们?
谢谢
【问题讨论】:
标签: angularjs controller components