Давайте посмотрим, как я должен работать ВЫБ.2. Моя цель была инициализировать ВЫБ.2 и добавить атрибут _ngcontent-
, чтобы чтобы стилизовать их через scss в моем объеме.
Html:
<select multiple="multiple" style="display: none;">
<option *ngFor="#item of people" [value]="item.id">
{{item.name}}
</option>
</select>
Init в Машинопись на ngAfterViewInit
:
ngAfterViewInit()
{
var element = (<any>$('select')).select2().siblings('.select2-container')[0];
var attribute = ObjectHelper.setElementContentAttribute(this.m_elementRef.nativeElement, element, true);
}
И специальные функции магии, чтобы клонировать атрибут к Чайлдс _ngcontent
. Очень полезно для многих третьих сторон библиотек, где генерируются некоторое динамическое содержание:
public static getAngularElementTag(element: Element): string
{
var attrs = [].filter.call(element.attributes, at => /^_nghost-/.test(at.name));
if (attrs.length == 0)
{
return null;
}
return attrs[0].name.replace("_nghost-", "_ngcontent-");
}
public static setElementContentAttribute(hostElement: Element, targetElement: Element, setRecursive?: boolean): string
{
var attribute = this.getAngularElementTag(hostElement);
setRecursive = (setRecursive !== undefined) ? setRecursive : false;
if (attribute !== null)
{
this._setElementContentAttribute(targetElement, setRecursive, attribute);
return attribute;
}
else
{
return null;
}
}
private static _setElementContentAttribute(targetElement: Element, recursive: boolean, attribute) {
targetElement.setAttribute(attribute, '');
if (recursive) {
for (var i = 0; i < targetElement.childElementCount; i++) {
this._setElementContentAttribute(<Element>targetElement.childNodes[i], recursive, attribute);
}
}
}
Это сделано только для стилизации входного элемента. Для динамически добавленных элементов (выбор, селектор) вам, вероятно, придется поймать некоторые события и снова сделать магию.
Возможно, вы нашли решение для этого? – Shahin