这永远是最好的选择
if (!($compileNodes instanceof jqLite)) {
// jquery always rewraps, whereas we need to preserve the original selector so that we can
// modify it.
$compileNodes = jqLite($compileNodes);
}
因此,如果它传递的不是 jqLite 元素 - 它会将其包装在 jqLite 元素中。
这允许传递内容、jqLite 元素或普通 DOM 元素。
在这里你可以在第 88 行看到$compileing HTML text:
element = $compile('<div></div>')($rootScope);
这里可以看到$compileing a jqLite element
element = jqLite('<div>{{1+2}}</div>');
$compile(element)($rootScope);
嗯?那不是文档!那是源头,你骗我的!
根据我的经验,Angular 在源代码中的记录比在在线文档中好多。该文档是最新的,并且所有类型和用法都存在。那里要简单得多:
- @param {string|DOMElement} element 要编译成模板函数的元素或 HTML 字符串。
虽然在这种情况下docs.angularjs 也做得不错,但导航时间更长。
这是 TypeScript 的一堆类型定义。它很有用,因为它可以很好地告诉您 - 类型 :)
interface ICompileService {
(element: string, transclude?: ITemplateLinkingFunction, maxPriority?: number): ITemplateLinkingFunction;
(element: Element, transclude?: ITemplateLinkingFunction, maxPriority?: number): ITemplateLinkingFunction;
(element: JQuery, transclude?: ITemplateLinkingFunction, maxPriority?: number): ITemplateLinkingFunction;
}
如您所见 - 这是我们在其他地方找到的三个选项。一个字符串、一个 Element 或一个 JQuery(通常是精简版)实例。