Angular 2 - Circular dependencies leading to the error message "unexpected value undefined"

Currently, I am organizing my app using shared, features, and core modules.

The structure of my SharedModule appears as follows:

@NgModule({
  declarations: [
    TendanceNotePipe, 
    ColorNotePipe,
  ],
  exports: [
    CommonModule,
    FormsModule,
    RouterModule,

    SearchBoxModule, // Requires FormsModule and the pipes TendanceNotePipe, ColorNotePipe

    TendanceNotePipe, 
    ColorNotePipe
  ]
})
export class SharedModule {}

The issue arises when the SearchBoxModule requires elements from the SharedModule, prompting me to import it within the SearchBoxModule:

@NgModule({
  imports: [
    SharedModule, // Contains Pipes and FormsModule required by components in this module
    HttpModule
  ],
  declarations: [
    SearchBoxComponent,
    ResultsBoxComponent,
    ResultsListComponent
  ],
  exports: [SearchBoxComponent]
})
export class SearchBoxModule { }

An error occurs:

Error: Unexpected value 'undefined' imported by the module SharedModule
. Is this possibly due to circular dependency?

Considering that the SearchBoxModule is a reusable component used multiple times across the app, including instances where it may be called twice in the same view, should it reside in the SharedModule?


I attempted to manually import the dependencies (Pipes and FormsModule) within the SearchBoxModule, but encountered another error:

Type TendanceNotePipe is part of the declarations of 2 modules: SharedModule and SearchBoxModule


The only workaround I found was moving the pipe declarations from SharedModule to SearchBoxModule, importing FormsModule there, and removing SharedModule from its list of imports.

However, this results in the pipes no longer being located in the SharedModule as intended!

What should be the course of action in this scenario?

Answer №1

To efficiently organize your code, consider creating two distinct modules for better separation of concerns. Alternatively, you could opt for a shared module approach by importing all dependencies necessary for the SearchBoxComponent in the SharedModule. Add the component to the declarations array and export it from the shared module, simplifying your project structure by eliminating the need for a separate searchbox module.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigate forward to the next available input in a grid by using the tab key

My goal is to improve form entry efficiency by using the tab key to focus on the next empty input field within a grid component. If an input field already has a value, it will be skipped. For example, if the cursor is in input field 2 and field 3 is filled ...

ngCordova camera functions properly on emulator, but encounters issues on actual devices

I am currently developing an app using Ionic for my Course Conclusion, and I am facing an issue with the ngCordova Camera Plugin. The deadline is approaching fast as I only have 2 weeks left to complete this project. While testing the app on the emulator, ...

Ways to share code across AngularJS frontend and Node.js backend

How can code be effectively shared between an AngularJS client and a Node.js server? After creating an AngularJS application, I now need to develop a RESTful server to provide data to the client. Some Angular services used on the client-side could also be ...

What is the reason behind the absence of a $interval feature in AngularJS?

One of the conveniences of AngularJS is its $timeout service, which serves as a wrapper for setTimeout. Have you ever wondered why there isn't an equivalent for setInterval in AngularJS? ...

Having trouble with creating SQLite tables using JavaScript within a for loop

I have developed a multi-platform app using AngularJS, JavaScript, Phonegap/Cordova, Monaca, and Onsen UI. In order to enable offline usage of the app, I have integrated an SQLite Database to store various data. After conducting some basic tests, I confir ...

Using Typescript for AngularJS bindings with ng.IComponentController

Currently, I am utilizing webpack alongside Babel and Typescript Presently, the controller in question is as follows: // HelloWorldController.ts class HelloWorldController implements ng.IComponentController { constructor(private $scope: ng.IScope) { } ...

Combining model with a string in an expression in AngularJS version 1

Can this text be transformed into an expression using ecmaScript 2015? The || operator seems to be causing issues. {{ ::$ctrl.realEstateProjectCurrentProduct.housingTax + ' €' || $ctrl.noDataMessage }} ...

npm ERROR: 404 - The package '@reactivex/rxjs' cannot be found in the npm registry

Encountering problems with the installation of this package through NPM. Attempting to install as it is a dependency of Angular2. Error: npm ERR! Darwin 15.0.0 npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "@reactivex/<a href="/cd ...

Retrieve an element from a child directive within the parent directive

Is there a way to retrieve the element with the class name .second from the second directive in the link function of the first directive? Click here P.S. I have tried it with the template in the link functions, but I specifically need to use templateUrl. ...

Issues with Angular ngroute: controllers are not functioning properly

In order to route my application with different themes on the same page, I plan to utilize the ngroute module. Here's an example of how I intend to achieve this: <html> <head> <title>Angular JS Views</title> ...

Guide to implementing code on a designated path with access to various services

I'm contemplating the best approach to configuring a route specifically designed to utilize a service call and then redirect. Currently, I've resorted to this workaround: $routeProvider.when('/talks', { template: '<div> ...

Having trouble reaching the elements stored in an array?

As a beginner in Angular and JavaScript, I may be making some obvious mistakes so please bear with me. I have written this code that allows the selection of 2 text files and then combines them into a single array. $scope.textArray = []; $scope.textUpload ...

Guide on how to direct a user to a specific page by clicking on an ngCordova local notification while the app is running in the

When receiving a local notification and clicking on it, the user is currently being sent to a specific view only when the app is in the foreground. However, if the app is closed and a notification is received, the user is redirected to the main page as def ...

Manually reloading the page causes issues with AngularJS routing functionality

I've been attempting to manually reload the page from my browser, but unfortunately it's not working and is displaying an error message: Cannot GET /rate/4 This is what my route setup looks like in Angular: angular.module('routing&ap ...

Obtaining an element through its id using an expression in an Angular directive

Here's a complex question that needs to be broken down. I'm trying to mimic the behavior of the native <label> for <input>. Since nesting is not an option, I use a style like this: <input type="checkbox" id="test" /> Some other ...

At what point is the rendering process of ng-switch completed?

I am currently utilizing ui-router and facing a challenge in instantiating a widget that requires a DOM element specified by its id as a parameter. The specific DOM element is nested within a <div ng-switch>, and I need to ensure the widget construct ...

Access Denied - NodeJS

Currently facing an issue with my MEAN stack application on AWS - Windows. I've set port 3000 for the Node server and IIS is using the default port 80. When trying to retrieve data using Angular via Node, I encounter an error while making a GET reque ...

Issue with AngularJS: The select dropdown fails to update the selected option when the bound scope variable changes

I am facing an issue with a select control in my Angular app. The options for the select are generated dynamically from an array of objects in the scope. When I try to pre-select a specific option on app init by changing a bound variable on the scope, it d ...

Utilizing AngularJS for enhanced data management in DataTables with a customized

I am currently working with AngularJs+DataTable library and I have a specific need to create a custom control that can utilize the exact search function from DataTable, but with a customized user interface. However, when using the search() method, it retur ...

Angular 9 is throwing an error that specifies that the options provided in the @ViewChild decorator must be in

After successfully upgrading my Angular project from version 8 to 9, I encountered an error when trying to run the project on localhost or build it. The error message states: ERROR in @ViewChild options must be an object literal The @ViewChild syntax that ...