Using Angular 2 to invoke a component

Currently, I am working on building a new Angular 2 application.

Within the home.component.ts file, there is a small form that has been designed as shown in the image linked below https://i.sstatic.net/nimDm.jpg

After the user clicks on the Submit button, the Datatable should load with the relevant results. This functionality is handled by a separate component called result.component.ts

You may refer to the appearance of the loaded Datatable by clicking on this link: https://i.sstatic.net/YcjCE.jpg

At this point, I am considering the best approach for invoking the result.component from within the home.component. It is important that the first component passes the resulting dataset to the second one seamlessly.

Answer №1

If you want to display the outcome based on its selector, which is specified in the @Component Directive, and transmit the result using an @Input:

result.component.ts

@Component({
  selector: 'search-result',
  teplateUrl: './result.component.html'
})
export class ResultComponent {
  @Input() result: Result;
}

home.component.ts

export class HomeComponent {
  searchString: string;
  result: Result;

  performSearch(searchString: string) {
    // Make API call here and store the result in this.result
  }
}

home.component.html

<label for="q"><strong>Territory:</strong></label><br>
<input type="text" name="q" ([ngModel])="searchString"><br>
<button (click)="performSearch(searchString)">Search</button>
Result:
<search-result result="result"></search-result>

result.component.html

<table>
  <tr *ngFor="let item of result">
    <td>{{ item }}
  </tr>
</table>

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

Navigation for GitHub pages

I've been working on this for what feels like forever. The persistent Error 404 I'm encountering is with the /Quest/questlist.txt file. https://i.sstatic.net/NYYRa.png Here's the code snippet I've been using: ``// QuestCarousel.tsx ...

Unable to reach the prototype of the object

I am encountering an issue while attempting to access the object's prototype in a Node.js code. The objective is to send this object through an API so that users can utilize its methods. The problem lies in the fact that the returned object only inclu ...

How can you switch the property of an object in VueJS?

I am currently working with an array called cars, which contains names of cars as well as a property called starred. My goal is to toggle the value of starred between true and false for each car, ensuring that only one car can have starred set to true at a ...

Adding a NVD3 chart into a Leaflet popup

My AngularJs web application incorporates a map built with Leaflet.js and Leaflet markercluster. For rendering charts, I utilize nvd3.js along with the nvd3-angular-directive. The data I have represents various countries worldwide, each displayed on the m ...

Executing a setInterval function in NodeJs without prolonging the process's lifespan

To better grasp what I need to do, I simplified the task at hand: we have a lengthy nodejs process with numerous queued async functions, making it impossible to predict when they will finish. Our goal is to create an update process that stores the current ...

Capture all HTTP requests made by Angular2

I'm trying to find a way to intercept and modify all HTTP requests made by Angular by adding custom headers. In previous releases prior to angular2 RC5, this was achieved by extending the BaseRequestOptions like this: class MyOptions extends BaseRequ ...

There was an issue with d3 version 4: the function this.setAttribute() was not

I am attempting to update the provided example to be compatible with d3 version 4 http://bl.ocks.org/ameyms/9184728 Most of the code has been successfully converted, but I am encountering difficulties with the following function: this.el.transition().del ...

How can JavaScript automatically calculate the sum of various numbers entered into multiple input fields?

I would like to automatically add different numbers entered in multiple input fields using JavaScript. Is there a way to calculate the sum of numbers entered in various input fields using JavaScript? "The input field consists of n numbers..." and I am ut ...

Steps for deploying an application using a private node module

I have my own custom library called mylib and an application that depends on this library named myapp. Whenever I deploy myapp, I am required to manually add mylib to the node_modules directory so it is uploaded with the app. However, every time I make m ...

What could be the reason my object is not re-rendering even after the state of the object has been updated

I am currently working on implementing an infinite scroll feature by iterating through my JSON data. Initially, I display the first 2 records upon loading the page. The goal is to update the object when a user scrolls to the bottom of the page in order to ...

Retrieving object properties dynamically using an array of strings

I am attempting to dynamically access an object property from an array of strings that contain the path to the desired value. Currently, it only accesses the first index ('Properties') and returns 'undefined' for the rest. I am expectin ...

Adjust form elements dynamically depending on the selected option in a dropdown menu using Ruby on Rails

I currently have the following database tables set up: Departments ID| Department_name | Manager_ID Employees ID| Name | Department_ID Salaries ID| Employee_ID | Salary_Amount The departments table holds information about different departments within t ...

Strategies for Handling a High Volume of API Requests in Node JS

My journey with Node JS began recently, and I had a smooth start. I followed an online tutorial and successfully accepted a GET request in my server.js file. Coming from a Java background, I found myself with a handful of questions that I couldn't fi ...

Tips for incorporating multiple services within a single Angular component

Issue found in src/app/header1/header1.component.ts:3:30 - TypeScript error TS2306: The file 'F:/Angular-projects/lawyer-listing/src/app/services/state.service.ts' is not recognized as a module. 3 import { StateService } from '../services/st ...

Animating Text Changes with JavaScript and jQuery

In my HTML, I have an input type="text" element and I want to attach an event handler that triggers when the text is changed. The issue arises because this input's value is dynamically updated by another JavaScript function, causing the event handler ...

Unable to detect JavaScript in Chrome Developer Tools

I am experiencing a strange issue where my JavaScript code is not showing in the sources window. When I include a debugger statement in my JS and then reload the page, it successfully breaks and I can view the JavaScript code. However, the tab is labeled ...

What is the right way to configure an Axios Interceptor in a ReactJS app using TypeScript?

While I typically use JS with React, I decided to switch to Typescript. However, I've been encountering an error when trying to initialize a request interceptor instance: src/utils/services/axios.service.ts:16:8 TS2322: Type '{ 'Content-Type ...

Create a Vue3 Component without attaching it to the DOM

Let's consider creating a Vue3 component in the following way: import { defineComponent } from "vue"; var instance = defineComponent({ computed:{ message() { return 'Hello world' } } }) To instantiate it and ...

Utilize user input to fetch data from an external API

Let's say there is a field for 'part number' input that is not enclosed in a form tag. Whenever a user enters a value, the onblur event or a button positioned next to the input field should trigger a query to an external site via its API and ...

Numbering the items in ng-repeat directive

I am facing a challenge with my AngularJS directive that is recursively nested within itself multiple times. The issue lies in the fact that the names of items in ng-repeat conflict with those in the outer element, causing it to malfunction. To illustrate ...