[英]TypeScript: How do I define a static array of strings in my directive
I have a directive for a colorpicker widget. 我有一个颜色选择器小部件的指令。 Currently I have defined an array of colors in the scope directly as scope.colorList as shown in the code below. 目前,我已经在scope中直接将颜色数组定义为scope.colorList,如下面的代码所示。
As per the review comments that I got,I want to declare a static array of colors rather than having to write in scope directly every time the widget is used. 根据我得到的评论,我想声明一个静态的颜色数组,而不是每次使用该小部件时都必须直接在范围内编写。
export class MyDirective implements ng.IDirective {
public link: (scope: IMyScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, ngModelCtnr: ng.INgModelController) => void;
constructor() {
var that = this;
this.link = (scope: IMyScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, ngModelCtnr: ng.INgModelController) => {
scope.colorList = ["#008b8b;", "#00bfff;", "#1766b5;", "#1768b5;", "#17b566;", "#1a7e55;", "#20b2aa;", "#25a071;", "#3b1153;", "#4f59ea;", "#4fc7ea;", "#522424;", "#633939;", "#6617b5;", "#68c4af;", "#7e1a43;", "#80e56f;", "#8b9dc3;", "#a560d6;", "#b56617;", "#b8860b;", "#ba55d3;", "#c0afaf;", "#c0c0c0;", "#c71585;", "#cd5c5c;", "#cec2e5;", "#dc143c", "#dcedc1;", "#f08080;", "#ff4040;", "#ffa500;", "#faebd7;"];
....
});
};
}
}
You can declare a static variable in your class. 您可以在类中声明一个静态变量。
export class MyDirective implements ng.IDirective {
private static COLOR_LIST = ["#008b8b;", "#00bfff;", "#1766b5;", "#1768b5;", "#17b566;", "#1a7e55;", "#20b2aa;", "#25a071;", "#3b1153;", "#4f59ea;", "#4fc7ea;", "#522424;", "#633939;", "#6617b5;", "#68c4af;", "#7e1a43;", "#80e56f;", "#8b9dc3;", "#a560d6;", "#b56617;", "#b8860b;", "#ba55d3;", "#c0afaf;", "#c0c0c0;", "#c71585;", "#cd5c5c;", "#cec2e5;", "#dc143c", "#dcedc1;", "#f08080;", "#ff4040;", "#ffa500;", "#faebd7;"];
public link: (scope: IMyScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, ngModelCtnr: ng.INgModelController) => void;
constructor() {
var that = this;
this.link = (scope: IMyScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, ngModelCtnr: ng.INgModelController) => {
scope.colorList = MyDirective.COLOR_LIST;
....
});
};
}
}
However, I would advise you use the constant recipe instead. 但是,我建议您改用常量配方 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.