`Where can I find resources on connecting components in Angular 2?`

Exploring ways to enhance my website, I am considering allowing users to customize the theme according to their preferences. To start off, I decided to introduce a 'Dark theme' option. In order to implement this feature effectively, I am working on integrating the dark theme separately. https://i.sstatic.net/BiWBZ.png

The screenshot above showcases a modal window where users can select the dark theme and modify the color scheme of the site. This interactive component is referred to as customize.component.html. Meanwhile, in the backdrop, you will find the display of the website results denoted by results.component.html.

customize.component.ts

<link type="text/javascript" href="../../assets/themes/theme.js" id="theme">
<div class="modal-dialog" role="document">
  <div class="modal-content">
    <div class="modal-header">
      <!-- Start ignoring HTMLLintBear -->
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <!-- Stop ignoring HTMLLintBear -->
      <h2 class="modal-title" id="my-modal-label">Customization</h2>
    </div>
    <div class="modal-body">
      <button class="theme-button" id="dark" onclick="darkTheme(title)">Dark Theme</button>
    </div>
  </div>
</div>

results.component.ts

<!-- Customization modal-box -->
<link type="text/javascript" href="../../assets/themes/theme.js">
<div class="modal fade" id="customization" tabindex="-1" role="dialog" aria-labelledby="myModallabel">
    <app-customize></app-customize>
</div>

This code snippet demonstrates both components. Additionally, I have created a file named theme.js where the theme processing functionality is defined. However, I am encountering issues with the execution of the code.

theme.js

 function darkTheme(id) {
    var el = document.getElementById(id);
    el.style.color = "red";
}

I am seeking guidance on how to access elements with an id from results.component.ts. I am currently facing challenges in implementing this specific feature. Your assistance would be greatly appreciated! Thank you!

NOTE - Angular Material 2 is not being utilized in my project, hence requiring an alternative method for enabling users to customize themes without Angular Material 2.

Answer №1

When working with Angular 2, it is recommended to steer clear of plain JavaScript linked through a link tag.

In your customise.component.ts file, ensure to include your darkTheme function. It's important to be able to use this function in other components as well. One way to achieve this is by using a service to store the data.

export class CustomiseComponent {
    constructor(
        private themeService: ThemeService
    ) {
    }

    darkTheme() {
        this.themeService.themeColor = 'red';
    }
}

@Injectable()
export class ThemeService{
    public themeColor: string;
}

customise.component.html

<div class="modal-dialog" role="document">
  <div class="modal-content">
    <div class="modal-header">
      <!-- Start ignoring HTMLLintBear -->
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
      <!-- Stop ignoring HTMLLintBear -->
      <h2 class="modal-title" id="my-modal-label">Customization</h2>
    </div>
    <div class="modal-body">
      <button class="theme-button" id="dark" (click)="darkTheme()">Dark Theme</button>
    </div>
  </div>
</div>

Take note of how the click binding is implemented: (click)="darkTheme()". You can then inject the same ThemeService into your ResultsComponent and utilize the themeColor field accordingly.

For instance: results.component.ts

export class ResultsComponent {
    constructor(
        public themeService: ThemeService
    ) {
    }
}

results.component.html

<label [style.color]="themeService.themeColor">Your colorful text</label>

This offers a potential solution to the issue, though in practical scenarios, using CSS class names may be preferred over colors.

Update

An example demonstrating the implementation of theming can be found at the following link:

https://embed.plnkr.co/PX1kcJAbNmBxTZmAzsiS/

In general, utilizing LESS or SASS can greatly simplify the definition of your themes.

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

Express/NodeJS encountering a CORS problem, causing services to be inaccessible in Internet Explorer

Currently, I am working on a REST-based service implemented in Express/NodeJS. The code includes CORS (Cross Origin Resource Sharing) implementation to allow services to be consumed from browsers like Chrome and Firefox. However, there seems to be an issue ...

How can I restrict Material UI TextFields with input type = file to only allow pdf files?

Is there a way to restrict users from uploading anything other than PDF files using input type=file? I've researched about using the accept attribute but seems like it's not compatible with material UI text fields. Is there an alternative soluti ...

Having difficulty creating JSON data following retrieval of rows by alias in MySQL

I am encountering an issue while trying to fetch rows from two tables using a JOIN and aliases. The problem arises when I attempt to convert the fetched rows into JSON data and assign them to a JSON array. Below is the code snippet: $personal = $db->p ...

The functionality of AngularJS routing is malfunctioning

I'm having trouble with implementing angularJS routing on my page. Initially, it was working fine but now the browser is not returning anything. Here's the code snippet: angular.module('myRoutingApp', ['ngRoute']) .conf ...

Centering loaders within elements of an unordered list

I am working on a navbar that uses Bootstrap 3 and AngularJS. Within this navbar, I have an unordered list with li elements. Below is a snippet of my navbar: https://i.stack.imgur.com/o75Lp.png This is the code for my navbar: <div class="navbar-head ...

Is there a way to carry out tests on keydown events within Jasmine by specifying the keyCode within an Angular2+

I am working on a project where I need to trigger keydown events on an <input> field. Tools Used Karma v1.7.1 as the test runner Tests running on Chrome browser Using Angular 5 framework Any insights on how I can achieve this? ...

Retrieve data from specified <td> element in an ASP.NET MVC application

Within my View, I am displaying data using a table. Below is the code snippet: @foreach (var item in Model) { <tr > // Table row content here... </tr> } I have buttons with ...

Accessing the Bootstrap4 Carousel control tabbing is hindered as the focus does not shift to the carousel-caption div when using the tab key on the

Check out this example using PFB for the Bootstrap Carousel control: Visit here to see the example in action. The example works well, but there seems to be an issue with tabbing when I try to navigate using the keyboard. The focus does not go to the anch ...

Should I use a single directive with ng-if statements in AngularJS, or opt for two separate directives?

Question about optimizing performance in Angular JS. I am using ng-repeat to display a list of search results. Each search item is extracted using a directive/template. Should I wrap two directives with ng-if statements or use one directive with multiple ...

Adjusting the left and right margins within a Bootstrap row nested inside a card component

I am struggling to adjust the left and right margins for a Bootstrap row inside a card. Below is my CSS code along with some screenshots for reference. <div class="container" style="margin-bottom: -20px;"> <div class=" ...

Twitter Bootstrap 3 - Change column order for extra small screens

My Twitter Bootstrap layout consists of two columns structured like this: <div class="col-md-4 col-sm-6 col-xs-12"> <div class="post"> Content here </div> </div> <div class="col-md-8 col-sm-6 col-xs-12"> <di ...

Running a JQuery function within an Angular expression

Can Angular expressions execute JQuery code? Here's an example: <span>{{$("[for='loanType']").text()}}</span> I attempted it, but haven't been successful so far. Is there a way to make it work without creating a key/value ...

Ensure that the content fills the entire height of the container and include a scrollbar

I'm currently utilizing CKEditor () and have developed my own customized file browser. The issue I'm encountering is that when the filebrowser is opened, it appears in a new popup window without scrollbars. I reached out for support through a ti ...

The jQuery UI Sortable functions are being triggered at lightning speed

I am currently working on a project where users can create a seating chart, add rows and tables, and move the tables between different rows. The functionality for adding rows and moving tables already exists in the code. However, I am facing an issue where ...

Is the Vuex mutation properly formatted?

Is the mutation method correctly written to modify the initial state array? I'm uncertain about the last few lines of the mutation method. What am I missing, if anything? // Storing state: { flights: [ {trip_class: 0, number_of_change="1"}, ...

Exploring the creation of an Angular service that utilizes HttpClient for making GET requests, with a focus on the different

I've been diving into Angular lately and I'm facing some challenges with handling get requests. If you're interested, you can check out my code on Angular Stackblitz import { HttpClient} from '@angular/common/http'; import { Inject ...

How to calculate the number of distinct elements using jQuery

Here is the HTML code I have: <input type="hidden" name="product_id" value = "1" class="product_id" /> <input type="hidden" name="product_id" value = "2" class="product_id" /> <input type="hidden" name="product_id" value = "5" class="produc ...

Deactivating upcoming weeks according to the year in Angular 8

In the user interface, there are dropdowns for selecting a year and a week. Once a year is selected, the list of weeks for that year is displayed in the week dropdown. Now, the requirement is to disable the selection of future weeks. For example, for the ...

Spacing between letters on a canvas element

This question is straightforward - I am struggling to find a way to adjust the letter spacing when drawing text on a canvas element. I want to achieve a similar effect as the CSS letter-spacing attribute, by increasing the space between each letter in the ...

I am experiencing difficulty with the color of my progress bar in Firefox

After successfully creating an interactive progress bar that works perfectly in Google Chrome and surprisingly also in Safari without vendor prefixes, I encountered a roadblock when testing it on Firefox. The progress bar color reverts back to the default ...