Skip to content
This repository was archived by the owner on Jan 22, 2018. It is now read-only.

Commit 6ee80f0

Browse files
author
Kamil Kisiela
committed
feat: ngModelAttrsManipulator helper
1 parent e1057cb commit 6ee80f0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/helpers/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import angular from 'angular';
2+
3+
function addIfNotPresent(nodes, attr, val) {
4+
angular.forEach(nodes, (node) => {
5+
if (!node.getAttribute(attr)) {
6+
node.setAttribute(attr, val);
7+
}
8+
});
9+
}
10+
11+
function getNgModelNodes(node) {
12+
const query = "[ng-model], [data-ng-model]";
13+
14+
return node.querySelectorAll(query)
15+
}
16+
17+
export function ngModelAttrsManipulator(template, options, attrName, attrValue) {
18+
const node = document.createElement('div');
19+
const skip = options.extras && options.extras.skipNgModelAttrsManipulator;
20+
21+
if (skip === true) {
22+
return template
23+
}
24+
node.innerHTML = template;
25+
const modelNodes = getNgModelNodes(node);
26+
27+
if (!modelNodes || !modelNodes.length) {
28+
return template;
29+
}
30+
31+
addIfNotPresent(modelNodes, attrName, attrValue);
32+
33+
return node.innerHTML;
34+
}

0 commit comments

Comments
 (0)