Angular 1.6 limits the ability to include multiple custom components on a single page

I'm currently working with angular 1.6 and have encountered an issue with two components, config-list and request-dates.

Both components function correctly individually on a page, but when I attempt to add both components to the same page, only the second one seems to work. In the example below, request-dates is the component that works properly.

Is it possible to have multiple components on a single page?

Below is the code for the main page:

<div ng-app="playground" ng-cloak>
    <config-list></config-list>
    <request-dates></request-dates>
</div>

<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/app/module.js"></script>
<script src="~/Scripts/app/config-list.component.js"></script>
<script src="~/Scripts/app/request-dates.component.js"></script>

For module.js:

(function () {
    "use strict";

    angular.module("playground", []);    
}());

As for config-list.component.js:

(function(){

    var module = angular.module("playground", []);

    function controller()
    {
        var model = this;
    };

    module.component("configList",
        {
            templateUrl: "/Scripts/app/config-list.component.html",
            controller: controller,
            controllerAs: "model"
        });
}());

Content of config-list.component.html:

<p>Hello from configlist</p>

Next is request-dates.component.js:

(function () {
    var module = angular.module("playground", []);

    function controller()
    {
        var model = this;
    }

    module.component("requestDates",
        {
            templateUrl: "/Scripts/app/request-dates.component.html",
            controller: controller,
            controllerAs: "model"
        });
}());

Lastly, here's request-dates.component.html:

<p>Hello from requestDates</p>

[Update] - The correct answer revealed that my issue was due to accidentally overwriting the module (replacing the first component) with a new module containing the second component. This explains why the first component was not displaying as expected.

Answer №1

When you are working with the playground module, you do not have to include the second parameter (dependencies).

In your file module.js, make sure you have

var module = angular.module("playground", []);
as this is the proper way to initialize a module.

For your files config-list.component.js and request-dates.component.js, simply reference your module instead of creating it again.

To reference an existing module:

var module = angular.module("playground");

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

AssertionError [ERR_ASSERTION]: The value of undefined is not equal to 390 in the GitLab

I'm a bit confused about the AssertionError [ERR_ASSERTION]: undefined == 390 in Gitlab. What I need is: The sumSalaries(obj) function, which should take an object obj as a parameter where the field names correspond to the employee's name and t ...

Autofill Text Input with React Material-UI

For my current project, I am utilizing Material UI and React. Each TextField in the project has a button next to it, and when the button is clicked, I want it to retrieve the value of its corresponding TextField and set that value as the input for another ...

Unable to successfully remove item by ID from mongoDB within a Next.js application

Currently, I am developing an application using NextJS for practice purposes. I'm facing challenges in deleting single data from the database with the findByIdAndDelete function. Error encountered: CastError: Cast to ObjectId failed for value "undef ...

What is the best way to rotate a cube when it is clicked on?

My current project involves rotating a cube by clicking on buttons either on the cube itself or floating next to it. At the moment, I have them floating for easier testing purposes, but placing them directly on the cube is not an issue. The main issue I&a ...

Event triggered only upon initial click

I am experiencing an issue with my category list. When I click on a category to trigger an AJAX request for the first time, the request does not go through. However, when I click on the same category a second time, it works perfectly. Below is the code sni ...

Unable to update the Angular scope variable directly inside an AJAX call

var request = { "RequestId": $scope.$parent.req_id, "RequestDate": new Date(), "RequestDetails": $scope.$parent.details }; $scope.request_status = "pending"; $.ajax({ type: "POST", url: ...

Loop through different JSON objects with unique values using ng-repeat

I am facing an issue with a JSON file that contains three different objects for each area, and I need some help. Here is an example: { "gebieden":"Antwerpen", "onderwerpen":"Gemiddeld netto inkomen per belastingsplichtige", "data_2005":"15084, ...

Sending state properties to components within a route

In my React structure, I have the following setup: <Provider store={ store }> <Router> <Switch> <Route path="/how-to" component={ Help } /> <Route path="/start" c ...

Monitoring a folder using webpack

Currently, I have webpack set up in my development environment to bundle all of my dependencies. To help concatenate various js files and include them in a script tag within the markup, I am utilizing the webpack-merge-and-include-globally plugin. Althoug ...

How to Eliminate ng-scope and ng-binding Attributes in AngularJS-generated HTML

Is it possible to programmatically remove the ng-scope and ng-binding attribute values that are dynamically added in the HTML generated by AngularJS? <tr ng-repeat="student in students" ng-class="isGrey[$index]" ng-click="toggleClass($index)" class=" ...

Utilizing group by date feature in Angular ag-Grid

I'm working on setting up an ag-grid with columns for date, time, and location. Below is the code snippet: list.component.ts columnDefs: any = [{ headerName: 'Date', field: 'date', valueFormatter: (data: any) => { ...

Obtain Page Parameters within a Nested Next.js Layout using App Router

My Next.js App Router has a nested dynamic page structure using catchall routes configured like this: app/stay/ |__[...category] | |__page.js |__[uid] | |__page.js |__layout.js Within the 'layout.js' file, there is a con ...

Troubleshooting: Issue with Updating Views in Angular JS

I've been attempting to change the view from the controller, but it's just not working properly. app.js var app = angular.module('vla', ['ngRoute']); app.config(function ($routeProvider){ $routeProvider ...

Connecting AngularJS controllers and services through data binding

I'm having trouble with setting up data binding between my controller and service. The issue seems to be with calling the factory method from other services and seeing the controller attributes update accordingly. I've tried different approaches ...

Error Message: Expecting an Object - Microsoft JScript Runtime Error in Node.js

I am starting my journey with Node JS and I encountered an unexpected error while running my code. Microsoft Jscript Runtime Error appeared, stating that "Object expected at line number 1". const fs = require('fs'); function FileObject () { thi ...

Unlocking the Secret: Effortless Transference of an AngularJS Scope Variable from Directive to Controller

Is there a simpler way to transfer an AngularJS scope variable from a directive to a controller? I've come across some complicated examples, but can't we directly access a controller from a directive and update one of its scope variables? ...

What is the best way to identify the property of an object that holds a specific value?

Searching through an object array in JavaScript to find a specific value and identify the property containing that value is my current task. Here's an example: Object Array: var objArray = [{ "ID": 1, "Name": "Kathy", "Position": "Progra ...

JavaScript function to provide a range of numbers based on a specified number and step value

I am looking for a solution to dynamically generate content based on the number of slides/steps using JavaScript. Any suggestions on how to best achieve this? Thanks in advance! switch(this.currentSlide) { case 1: return '1-12'; ...

Navigating through search results on an XML page using jQuery

Context: I am currently facing a challenge involving the integration of Google search outcomes into a webpage I'm constructing. These findings are presented in XML format. My current approach to importing the XML is as follows: if (window.XMLHttpReq ...

GeckoDriver Firefox and Protractor(Selenium) encountered a NoSuchWindowError due to the browsing context being discarded

I need assistance with running a basic test script using protractor. Environment: Node Version: v9.8.0 Protractor Version: 5.4.1 Angular Version: 1.x Browser(s): Mozilla Firefox 60.1.0 Operating System and Version: HELiOS release 6.10 Below is my config ...