我有这样的模式:Angular2类没有暴露功能
(function(app) {
app.productLine =
ng.core.Component({
selector: 'product-line',
templateUrl: 'templates/sales-product.html',
})
.Class({
constructor: function() {
this.products = [new app.product('101010101010101', '1', '19.99')];
},
addLine: function() {
this.products.push(new app.product('101010101010101', '1', '19.99'));
}
});
})(window.app || (window.app = {}));
(function(app) {
app.product = function (upc, quantity, price) {
this.upc = upc;
this.quantity = quantity;
this.price = price;
return this;
}
})(window.app || (window.app = {}));
但是,我想不出如何公开addLine()
这样我就可以在其他地方调用它。
记录PRODUCTLINE只能显示构造:
console.log(app.productLine);
function app.productLine<.constructor()
,并呼吁app.productLine.addLine()
给TypeError: app.productLine.addLine is not a function.
编辑:
我发现,添加addLine
功能app.productLine
直接做工作。当然,this
的范围会发生变化,所以需要在更明显的位置引用构造函数的结果。您可以运行app.productLine.add(123,456,789);
并更新型号。
但是,该视图不会立即更新。我认为有必要以某种方式触发更新,但使用双向数据绑定时,如果使用UI更新模型,则会显示所有更新。
哇,我甚至没有在文档中找到“输入”。我一定错过了。很高兴我能帮你打破15k - 恭喜! – Josiah