Skip to content

Commit 61bbcef

Browse files
committed
for controllers
1 parent b8d0b87 commit 61bbcef

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
}
1515
}
1616
class TodoController {
17+
static $scopename = "test";
1718
constructor() {
1819
this.greeting = "Todos";
1920
this.model = {

src/angular.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ export class Angular {
304304
*
305305
* Internally, this walks down the `Scope` tree starting from `$rootScope`
306306
* and checks for a matching `$scopename` property. The `$scopename` property
307-
* may be defined statically or assigned via the `ngScope` directive.
307+
* may be defined statically on controllers using `as` syntax, assigned via the `ngScope` directive,
308+
* or defined on `$scope` injectable.
308309
*
309310
* @param {string} name
310311
* @returns {ProxyHandler<ng.Scope>|undefined}

src/angular.spec.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ describe("angular", () => {
99

1010
beforeEach(() => {
1111
angular = new Angular();
12-
module = angular.module("defaultModule", ["ng"]);
13-
injector = createInjector(["ng", "defaultModule"]);
12+
module = angular.module("default", ["ng"]);
13+
injector = createInjector(["ng", "default"]);
1414
$rootScope = injector.get("$rootScope");
1515
$compile = injector.get("$compile");
1616
});
@@ -215,8 +215,8 @@ describe("angular", () => {
215215

216216
beforeEach(() => {
217217
angular = new Angular();
218-
module = angular.module("defaultModule", ["ng"]);
219-
injector = createInjector(["defaultModule"]);
218+
module = angular.module("default", ["ng"]);
219+
injector = createInjector(["default"]);
220220
$rootScope = injector.get("$rootScope");
221221
$compile = injector.get("$compile");
222222
});
@@ -246,8 +246,13 @@ describe("angular", () => {
246246

247247
beforeEach(() => {
248248
angular = new Angular();
249-
module = angular.module("defaultModule", ["ng"]);
250-
injector = createInjector(["defaultModule"]);
249+
module = angular.module("default", ["ng"]).controller(
250+
"demo",
251+
class Demo {
252+
static $scopename = "demo";
253+
},
254+
);
255+
injector = createInjector(["default"]);
251256
});
252257

253258
it("should return named scope", () => {
@@ -264,6 +269,22 @@ describe("angular", () => {
264269

265270
expect(angular.getScopeByName("fail")).toBeUndefined();
266271
});
272+
273+
it("should return controllers with static $scopename property", () => {
274+
const element = createElementFromHTML("<div ng-controller='demo'></div>");
275+
angular.bootstrap(element, ["default"]);
276+
277+
expect(angular.getScopeByName("demo")).toBeDefined();
278+
});
279+
280+
it("should return controllers with static $scopename property registered with `as` syntax", () => {
281+
const element = createElementFromHTML(
282+
"<div ng-controller='demo as $ctrl'></div>",
283+
);
284+
angular.bootstrap(element, ["default"]);
285+
286+
expect(angular.getScopeByName("demo")).toBeDefined();
287+
});
267288
});
268289
});
269290

src/core/compile/compile.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10488,6 +10488,7 @@ describe("$compile", () => {
1048810488

1048910489
expect(childCtrl.fromParent1).toBe(parentCtrl.value1);
1049010490
expect(childCtrl.fromParent1).not.toBe(childCtrl.value1);
10491+
debugger;
1049110492
expect(childCtrl.fromParent2).toBe(parentCtrl.value2);
1049210493
expect(childCtrl.fromParent2).not.toBe(childCtrl.value2);
1049310494
expect(childCtrl.fromParent3()()).toBe(parentCtrl.value3());

src/core/controller/controller.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ export class ControllerProvider {
123123
);
124124
}
125125

126+
if (instance?.constructor?.$scopename) {
127+
locals.$scope.$scopename = instance.constructor.$scopename;
128+
}
129+
126130
return function () {
127131
const result = $injector.invoke(
128132
expression,

0 commit comments

Comments
 (0)