Error: _co.function is not a valid function reference

Currently, I am working with Angular and attempting to create a webpage on my localhost. However, I am facing an issue where I am unable to use functions. I suspect it might be a path problem, even though I have correctly declared all the paths.

I spent about 4 hours searching online for a solution but couldn't find any relevant information addressing this particular issue.

In addition to the default components created when setting up the Angular project, I have also made two components myself:

post-create.component.html

post-create.component.ts

Both of these components are located in the following path from the app folder:

'./posts/posts-create/post-create.component.html'
'./posts/posts-create/post-create.component.ts'

I double-checked the names of the folders to ensure there are no spelling mistakes.

The code in post-create.component.ts looks like this:

import { Component } from '@angular/core';


@Component({
  selector: 'app-post-create',
  templateUrl: './post-create.component.html'
})
export class PostCreateComponent{
  newPost = '';

  onAddPost(){
    this.newPost = 'The users posts';
  }
}

The content in post-create.component.html is just some random text in an attempt to make it function, as shown below:

<h2> sup </h2>

The content of my app.component.html is:

<h1> Hello world </h1>
<textarea rows="6"></textarea>
<hr>
<button (click)="onAddPost()"> Save post </button>
<p>{{ newPost }}</p>

When I click the button, it should trigger the function "onAddPost()", however, it does not work and instead gives me this error message: ERROR TypeError: _co.onAddPost is not a function

I am unsure why this error is occurring. The content of my app.module.ts file is as follows:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { PostCreateComponent } from './posts/posts-create/post-create.component'

@NgModule({
  declarations: [
    AppComponent,
    PostCreateComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

As you can see, I have imported

import { PostCreateComponent } from './posts/posts-create/post-create.component'
And included it in the declerations section.

This code is part of a tutorial video that I am following along with. If you are curious, the video link is provided here: https://www.youtube.com/watch?v=1tRLveSyNz8&fbclid=IwAR0k3FKxqbYVKuT3g2UgJVQWFW2xFXV8xmhzGbwqbyZ3ltWrBcVktYc38_I

Thank you in advance for any assistance or insights you may provide. I apologize for taking up your time, but despite my efforts to resolve this issue on my own, I seem to be unable to determine why the function is not working.

Answer №1

Don't miss out on rewatching the video starting at 1:02:00 :

The information below pertains to post-create.component.ts (not app.component.ts) :

<h1> Hello world </h1>
<textarea rows="6"></textarea>
<hr>
<button (click)="onAddPost()"> Save post </button>
<p>{{ newPost }}</p>

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

How to use jQuery to retrieve the value of a select box based on the text it

Below is an example of a select box: <select id="year"> <option value="1">1 Year</option> <option value="2">2 Years</option> <option value="3">3 Years</option> <option va ...

Utilizing the Context Object in a Slack API Bolt Project for seamless property transfer between methods

I'm currently working on a Slack app using the JavaScript Bolt framework. This app essentially listens for specific message keywords in channels and then forwards those messages to the users of the app. My main goal is to add a permalink to the forwa ...

Formatting dates in the Bootstrap Date Picker within Angular 6

When using Angular 6, I incorporate a date picker by adding the bsDaterangepicker class for selecting a date range. <input type="text" (ngModelChange)="dateFilterChanged($event)" [(ngModel)]="myDateField" value="{{ myDateField | date:'yyyy-MM-dd&a ...

Error message displayed when refreshing a page using ngRoute: "Page cannot be found."

Having an issue with angularjs ngRoute for routing a single page application. When attempting to reload the page at localhost/about or localhost/blog, I receive an error message stating "Cannot GET /about". How can this be resolved? Below is my router con ...

Having trouble making the jQuery "simulate width: calc(100%)" function work properly?

I am utilizing a combination of HTML, CSS, mediaQuery, Javascript, jQuery, and PrimeFaces in my project. One particular css property I want to use is: calc(100% - 100px) To handle old browsers that do not support this property, I have implemented a javas ...

Step-by-Step Guide: Crafting a Non-Ajax Popup Chat Application

Recently, I created a unique dating website with a one-to-one chat feature similar to Facebook. However, I implemented this using the ajax technique and the setInterval function in JavaScript for regular updates. Upon reflection, I believe that this appr ...

How to Keep Bootstrap 3 Accordion Open Even When Collapsed

I am in the process of building an accordion group with bootstrap 3. Here is the code I have so far: <div id="accordion" class="panel-group"> <div class="panel panel-default"> <div class="panel-heading"> <div class ...

Updating a div using AJAX in Rails 4

Looking for a way to update a table with new values using AJAX? If you have a cart where users can change the quantity of products without reloading the entire page, consider this approach. Currently, the table reloads along with the whole page. Here&apos ...

Clarification on Regular Expressions

Looking to improve my java-script code for extracting words that occur after an "=" in a string. For example: string strOld="ActionDate=11/20/2014 12:00:00 AM "; strOld.substr(strOld.indexOf(type)).substr(type.length +1, strOld.substr(strOld.indexOf(typ ...

Dealing with Angular CORS Problems While Sending Successive Requests

I am currently working with Angular 6 and my backend consists of a node API. Occasionally, I encounter a CORS issue while making HTTP GET requests every 5 seconds. const url = 'https://<removed>.com/api/v1/transactions/' + transactionI ...

reCAPTCHA 2 triggers an error of 'excessive recursion'

I have decided to incorporate bootstrap 4 into my current Umbraco CMS project. My goal is to integrate reCAPTCHA 2 into a registration form, but I am encountering some difficulties in the process. I have successfully generated the keys for the reCAPTCHA ...

JavaScript slice() method displaying the wrong number of cards when clicked

In my code, I have a section called .registryShowcase. This section is designed to display logos and initially shows 8 of them. Upon clicking a button, it should load the next set of 8 logos until there are no more left to load (at which point the button ...

Selecting checkboxes

If you're looking for an example of limiting check boxes to a specific number, you can check out the code snippet here. var limit = 3; $('input.single-checkbox').on('change', function(evt) { if ($(this).siblings(':che ...

Implementing a button disable feature in Vuejs until email validation is complete

new Vue({ el: '#app', data: { email: '' }, computed: { isEmailValid() { return '/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\ ...

Trouble with apostrophes rendering in JavaScript on WordPress posts

My current challenge involves adding a post to Wordpress using an external script. This post includes a JavaScript section that contains an iframe for displaying movies. Knowing that Wordpress splits default tags, I have implemented a special plugin to han ...

React Native Function fails to return a value

Within my React Native app, there's a page called RepsPage that displays a scroll view using an array of IDs. I'm passing this array to a function named renderRep, attempting to create a view for each item in the array. However, the return statem ...

Maximizing Efficiency: Sending Multiple Responses during computation with Express.js

Seeking a way to send multiple responses to a client while computing. See the example below: app.get("/test", (req, res) => { console.log('test'); setTimeout(() => { res.write('Yep'); setTime ...

the functionality of the multiple dropdown in JavaScript only functions once

Hi everyone, I am new to JavaScript and I am seeking assistance with a dropdown issue I am facing. Currently, I am using a JavaScript code for multi-level dropdowns. However, when I use it for multiple dropdowns, the second dropdown follows the sequence of ...

What are some ways to troubleshoot a jQuery object?

Similar Question: Debugging JavaScript Occasionally, I encounter a jQuery object and struggle to ascertain its contents. I find myself experimenting with various methods such as inspecting the class name, ID, and other attributes or hiding it in an at ...

Svelte remains static and is not experiencing re-rendering

I am currently incorporating the history component in Svelte, where it should display when changes occur in the bids array. const bidsArray = [ { player: 0, bidType: 'pass' }, { player: 1, bidType: 'level', level: '1&apos ...