I am facing an issue with handling an EventEmitter in my child component, which is a list of devices. The parent component displays details of the selected device and I want to be able to change the selected device when needed. However, I keep getting an error message that says:
Unexpected directive value 'undefined' on the View of component 'ListOfDevicesComponent'
The complete console log shows:
EXCEPTION: Unexpected directive value 'undefined' on the View of component 'ListOfDevicesComponent'
angular2.dev.js:23083 EXCEPTION: Unexpected directive value 'undefined' on the View of component 'ListOfDevicesComponent'BrowserDomAdapter.logError @ angular2.dev.js:23083BrowserDomAdapter.logGroup @ angular2.dev.js:23094ExceptionHandler.call @ angular2.dev.js:1185(anonymous function) @ angular2.dev.js:12591NgZone._notifyOnError @ angular2.dev.js:13515collection_1.StringMapWrapper.merge.onError @ angular2.dev.js:13419Zone.run @ angular2-polyfills.js:1247(anonymous function) @ angular2.dev.js:13438zoneBoundFn @ angular2-polyfills.js:1220lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:468lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:480lib$es6$promise$$internal$$publish @ angular2-polyfills.js:451(anonymous function) @ angular2-polyfills.js:123microtask @ angular2.dev.js:13470Zone.run @ angular2-polyfills.js:1243(anonymous function) @ angular2.dev.js:13438zoneBoundFn @ angular2-polyfills.js:1220lib$es6$promise$asap$$flush @ angular2-polyfills.js:262
angular2.dev.js:23083 STACKTRACE:BrowserDomAdapter.logError @ angular2.dev.js:23083ExceptionHandler.call @ angular2.dev.js:1187(anonymous function) @ angular2.dev.js:12591NgZone._notifyOnError @ angular2.dev.js:13515collection_1.StringMapWrapper.merge.onError @ angular2.dev.js:13419Zone.run @ angular2-polyfills.js:1247(anonymous function) @ angular2.dev.js:13438zoneBoundFn @ angular2-polyfills.js:1220lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:468lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:480lib$es6$promise$$internal$$publish @ angular2-polyfills.js:451(anonymous function) @ angular2-polyfills.js:123microtask @ angular2.dev.js:13470Zone.run @ angular2-polyfills.js:1243(anonymous function) @ angular2.dev.js:13438zoneBoundFn @ angular2-polyfills.js:1220lib$es6$promise$asap$$flush @ angular2-polyfills.js:262
angular2.dev.js:23083 Error: Unexpected directive value 'undefined' on the View of component 'ListOfDevicesComponent'
at new BaseException (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:7351:21)
at RuntimeMetadataResolver.getViewDirectivesMetadata (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:22367:17)
at TemplateCompiler._compileNestedComponentRuntime (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:24329:63)
at http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:24314:26
at Array.forEach (native)
at http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:24313:37
To troubleshoot, I commented out some code lines to isolate the problem. It appears that the error originates from this line in my child component:
private selectDevice: EventEmitter<Device> = new EventEmitter();
Removing this line resolves the error. Below are relevant parts of my code related to this issue for reference.
This snippet is from my ListOfDevicesComponent (child component):
@Component({
// Other properties...
inputs: ['selectedDevice'],
outputs: ['selectDevice'],
directives: [
DeviceOverviewComponent,
ROUTER_DIRECTIVES]
})
export class ListOfDevicesComponent {
private devices = this._devicesServices.getDevices();
private selectedDevice: Device;
private selectDevice: EventEmitter<Device> = new EventEmitter();
constructor(private _devicesServices: DevicesServices) {}
onSelect(device: Device) {
this.selectDevice.next(device);
}
}
The corresponding template section:
<tbody>
<tr *ngFor="#device of devices" [class.active]="device === selectedDevice" (click)="onSelect(device)">
<device-overview [currentDevice]="device" ></device-overview>
</tr>
</tbody>
Snippet from my DeviceDetailsComponent (parent component):
@Component({
// Other properties...
directives: [
ListOfDevicesComponent,
ROUTER_DIRECTIVES]
})
export class DeviceDetailsComponent {
private devices = this._devicesServices.getDevices();
private selectedDevice: Device;
constructor(private _devicesServices: DevicesServices) {}
handleSelectDevice(device) {
this.selectedDevice = device;
}
}
It integrates the child component like this:
<list-of-devices [selectedDevice]="device" (selectDevice)="handleSelectDevice($event)"></list-of-devices>
Any assistance or insights would be greatly appreciated!