Skip to content

Commit b82ca92

Browse files
committed
Normalize name
1 parent 20f6c1f commit b82ca92

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/directive/observe/observe.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isUndefined } from "../../shared/utils.js";
1+
import { kebabToCamel } from "../../shared/jqlite/jqlite.js";
22

33
/**
44
* @param {string} source - the name of the attribute to be observed
@@ -10,20 +10,21 @@ export function ngObserveDirective(source, prop) {
1010
restrict: "A",
1111
compile: () => (scope, element) => {
1212
const targetElement = element[0];
13-
if (isUndefined(prop) || prop == "") {
13+
if (prop === "") {
1414
prop = source;
1515
}
16-
if (!scope[prop]) {
17-
scope[prop] = targetElement.getAttribute(source);
16+
const normalized = kebabToCamel(prop);
17+
if (!scope[normalized]) {
18+
scope[normalized] = targetElement.getAttribute(source);
1819
}
1920

2021
const observer = new MutationObserver((mutations) => {
2122
const mutation = mutations[0];
2223
const newValue = /** @type {HTMLElement} */ (
2324
mutation.target
2425
).getAttribute(source);
25-
if (scope[prop] !== newValue) {
26-
scope[prop] = newValue;
26+
if (scope[normalized] !== newValue) {
27+
scope[normalized] = newValue;
2728
scope.$digest();
2829
}
2930
});

src/directive/observe/observe.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe("observe", () => {
8989
});
9090

9191
it("should observe attribute changes and update the same scope name if data-update attribute is absent", () => {
92-
$scope.myProp = "";
92+
$scope.testAttribute = "";
9393
const template = `<div ng-observe-test-attribute></div>`;
9494
element = $compile(template)($scope);
9595
$scope.$digest();
@@ -107,5 +107,6 @@ describe("observe", () => {
107107

108108
mutationObserverCallback([mutationRecord]);
109109
expect($scope.$digest).toHaveBeenCalled();
110+
expect($scope.testAttribute).toBe("newValue");
110111
});
111112
});

0 commit comments

Comments
 (0)