我有一个输入字段gradingKeys
,我从父组件传递数据。
只有在传递数据时才有效,但我在最初没有gradingKeys
数组的不同地方使用该组件。
export class GradingKeyComponent {
groupedGradingKeys: any[];
@Input() set gradingKeys(gradingKeys: any[]) {
if (gradingKeys) {
let query = // do stuff
this.groupedGradingKeys = query;
}
}
}
我认为当我使用setter时,我可以更好地控制子组件的输入评估,因为我在遇到相同错误之前使用了ngOnChanges
事件。
我不想检查gradingKeys
是否是真实的......也是可行的!
在我的场景中,我应该尝试什么角度2方法?
错误
error_handler.js:45EXCEPTION: Error in ./TestsCreateComponent class TestsCreateComponent - inline template:131:20 caused by: Cannot read property 'Symbol(Symbol.iterator)' of undefinedErrorHandler.handleError @ error_handler.js:45
error_handler.js:47ORIGINAL EXCEPTION: Cannot read property 'Symbol(Symbol.iterator)' of undefinedErrorHandler.handleError @ error_handler.js:47
error_handler.js:50ORIGINAL STACKTRACE:ErrorHandler.handleError @ error_handler.js:50
error_handler.js:51TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
at EnumerableImpl.[Symbol.iterator] (linq.js:122)
at EnumerableImpl.Select [as _factory] (generators.js:27)
at Select.next (<anonymous>)
at Object.getKeyedMap (utilities.js:47)
at EnumerableImpl.GroupBy (linq.js:422)
at GradingKeyComponent.set [as gradingKeys] (grading-key.component.ts:19)
at _View_TestsCreateComponent1.detectChangesInternal (TestsCreateComponent.ngfactory.js:788)
at _View_TestsCreateComponent1.AppView.detectChanges (view.js:272)
at _View_TestsCreateComponent1.DebugAppView.detectChanges (view.js:377)
at _View_TestsCreateComponent0.AppView.detectContentChildrenChanges (view.js:290)ErrorHandler.handleError @ error_handler.js:51
error_handler.js:54ERROR CONTEXT:ErrorHandler.handleError @ error_handler.js:54
error_handler.js:55DebugContextErrorHandler.handleError @ error_handler.js:55
Subscriber.js:227Uncaught ViewWrappedError