Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Potential bug in registerComponent #14391

@rszachtsznajder

Description

@rszachtsznajder

Description

Bug exists on angular 1.5.3 and even on master branch.

In compile.js at the beginning of registerComponent (angular.component) method there is a assignment:

var controller = options.controller || noop;

Some lines later (after factory function definition) the controller variable is modified by assigning component options starting with $ sign.

forEach(options, function(val, key) {
  if (key.charAt(0) === '$') {
    factory[key] = val;
    // Don't try to copy over annotations to named controller
    if (isFunction(controller)) controller[key] = val;
  }
});

This may modify an angular method noop if component doesn't have a controller, which should not have happened.

How I found it?

I came across this problem using ngComponentRouter 2.0.0 with Angular 1.5.3 when I had two components without controllers defined. In this situation when factory function is executed, the default controller (noop) has $routeConfig property from the later one defined component (because registerComponent modify the same object: noop function). Result: ngComponentRouter couldn't find properly defined route.

Fix proposal

Probably the fix is fairly simple. Use function() {} instead of noop:

var controller = options.controller || function() {};

This will guarantee new function object at any situation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions