看起來像組合TS和角度時,我在控制器上的所有東西都暴露在視圖中。在我的情況下,myPrivate
將出現在$ctrl
。字體私人和受保護的成員暴露在角度1.x視圖
class MyController extends BaseController implements SomeInterface {
private myPrivate: string = 'myPrivateString';
}
是否有解決此問題的任何解決方法?
看起來像組合TS和角度時,我在控制器上的所有東西都暴露在視圖中。在我的情況下,myPrivate
將出現在$ctrl
。字體私人和受保護的成員暴露在角度1.x視圖
class MyController extends BaseController implements SomeInterface {
private myPrivate: string = 'myPrivateString';
}
是否有解決此問題的任何解決方法?
這很明顯,爲什麼當你看着生成的JavaScript。
var MyController = (function (_super) {
__extends(MyController, _super);
function MyController() {
_super.apply(this, arguments);
this.myPrivate = 'myPrivateString';
}
return MyController;
}(BaseController));
你的私有財產作爲你的控制器上的任何其他財產結束。
您可以看到完整的轉錄here。
一個解決方案將是有一個參數化的基本控制器能夠爲視圖設置類似視圖模型的東西,而不是常規的$ ctrl。
這將是這個樣子:
class BaseController<T> {
protected scope;
protected viewModel: T;
constructor(scope: any, modelType: { new(): T; }) {
this.scope = scope;
this.viewModel = new modelType();
this.scope["viewModel"] = this.viewModel;
}
}
class MyParticularViewModel {
public somethingForTheView: string;
}
class MyController extends BaseController<MyParticularViewModel> implements SomeInterface {
private myPrivate: string = 'myPrivateString';
constructor(scope) {
super(scope, MyParticularViewModel);
}
}
在視圖中,那麼你可以使用視圖模型屬性來訪問需要的屬性。
我在實踐中已經在一個項目中使用了它,它的工作非常好。您可以看到我使用here獲得更多信息的初學者模板。
你試過一個參數化的構造函數來初始化你的私人成員? – gkb
我不想只是歧視,但也阻止實際使用。 – jvrnt
這實際上是非常可靠的IMO參數。謝謝 – jvrnt