简体   繁体   English

未捕获的ReferenceError:函数未使用带有角形式和js的onclick定义

[英]Uncaught ReferenceError: function is not defined with onclick with angular form and js

I've tried a few different ways to solve this but no luck: 我尝试了几种不同的方法来解决此问题,但没有运气:

  1. put script on same html page 将脚本放在相同的html页面上
  2. added event listener 添加了事件监听器
  3. moved script inside body function 身体功能内移动的脚本
  4. adjusting <script src=""></script> 调整<script src=""></script>

Using Developer tools it looks like I'm hitting my onclick function in the html file but it's not hitting my js file. 使用开发人员工具,好像我在html文件中命中了onclick函数,但没有命中我的js文件。

component html file 组件html文件

    <body>
    <form (ngSubmit)="onSubmit(f)" #f="ngForm">
    <div class="row">
      <div class="col-lg-12 form-group">
      <label for="email">Email</label>
      <input
        type="email"
        id="email"
        class="form-control"
        name="email"
        ngModel
        required
        [pattern]="'^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$'"
        >
    </div>
      <div class="row moveRight">
        <div class="col-lg-12 form-group">
      <label for="firstname">First Name</label>
      <input
        type="text"
        id="firstname"
        class="form-control"
        name="firstname"
        ngModel
        required
        >
      </div>
      </div>
      <div class="row moveRight">
        <div class="col-lg-12 form-group">
          <label for="lastname">Last Name</label>
          <input
            type="text"
            id="lastname"
            class="form-control"
            name="lastname"
            ngModel
            required
            >
        </div>
      </div>
      <div class="row">
        <div class="col-lg-12">
          <button class="btn btn-success" id="startnode" type="submit" [disabled]="!f.valid" onclick="submitform()">Submit</button>
        </div>
      </div>
    </div>
  </form>
  <script type="text/javascript" src="form.js">
  </script>
</body>

js file js文件

$(document).ready(function(){

  function submitform() {

var https = require('https');
var querystring = require('querystring');

var postData = querystring.stringify({
  'email': req.body.email,
  'firstname': req.body.firstname,
  'lastname': req.body.lastname,
  'hs_context': JSON.stringify({
    "hutk": req.cookies.hubspotutk,
    "ipAddress": req.headers['x-forwarded-for'] || req.connection.remoteAddress,
    "pageUrl": "http://hubspot-form-for-interview-boston.s3-website.us-east-2.amazonaws.com/",
    "pageName": "Example Title"
  })
});


var options = {
  hostname: 'forms.hubspot.com',
  path: '/uploads/form/v2/HUBID/FORMID',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
}


var request = https.request(options, function (response) {
  console.log("Status: " + response.statusCode);
  console.log("Headers: " + JSON.stringify(response.headers));
  response.setEncoding('utf8');
  response.on('data', function (chunk) {
    console.log('Body: ' + chunk)
  });
});

request.on('error', function (e) {
  console.log("Problem with request " + e.message)
});


request.write(postData);
request.end();
  }


});

document.getElementById ("startnode").addEventListener ("click", submitform, false);

In Angular, we normally put our data access code in a service. 在Angular中,我们通常将数据访问代码放入服务中。 Here is an example: 这是一个例子:

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/map';

import { IProduct } from './product';

@Injectable()
export class ProductService {
    private baseUrl = 'api/products';

    constructor(private http: Http) { }

    saveProduct(product: IProduct): Observable<IProduct> {
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });

        const url = `${this.baseUrl}/${product.id}`;
        return this.http.put(url, product, options)
            .map(() => product)
            .do(data => console.log('updateProduct: ' + JSON.stringify(data)))
            .catch(this.handleError);
    }

    private handleError(error: Response): Observable<any> {
        // in a real world app, we may send the server to some remote logging infrastructure
        // instead of just logging it to the console
        console.error(error);
        return Observable.throw(error.json().error || 'Server error');
    }
}

And then the component calls this service to save data: 然后组件调用此服务以保存数据:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { IProduct } from './product';
import { ProductService } from './product.service';

@Component({
    templateUrl: './app/products/product-edit.component.html',
    styleUrls: ['./app/products/product-edit.component.css']
})
export class ProductEditComponent implements OnInit {
    pageTitle: string = 'Product Edit';
    errorMessage: string;
    product: IProduct;

    constructor(private router: Router,
        private productService: ProductService) { }

    saveProduct(): void {
        if (this.isValid(null)) {
            this.productService.saveProduct(this.product)
                .subscribe(
                    () => this.onSaveComplete(`${this.product.productName} was saved`),
                    (error: any) => this.errorMessage = <any>error
                );
        } else {
            this.errorMessage = 'Please correct the validation errors.';
        }
    }

    onSaveComplete(message?: string): void {
        // Navigate back to the product list
        this.router.navigate(['/products']);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的 ReferenceError:未使用 onclick 定义函数 - Uncaught ReferenceError: function is not defined with onclick 未捕获的ReferenceError:onclick上未定义函数 - Uncaught ReferenceError: function not defined at onclick Angular 2 和 LeafLet 未捕获的 ReferenceError:<function> 未在 HTMLButtonElement.onclick 中定义 - Angular 2 and LeafLet Uncaught ReferenceError: <function> is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:在HTMLButtonElement.onclick中未定义函数 - Uncaught ReferenceError: function is not defined at HTMLButtonElement.onclick 未捕获的参考错误:<function> 未在 HTMLInputElement.onclick 中定义 - Uncaught ReferenceError: <function> is not defined at HTMLInputElement.onclick 未捕获的ReferenceError:onclick函数Javascript上未定义变量 - Uncaught ReferenceError: variable is not defined on onclick function Javascript 未捕获的 ReferenceError:未定义 function。 单击/更改 - Uncaught ReferenceError: function is not defined. Onclick/ onChange 未捕获的ReferenceError :(函数)未在HTMLButtonElement.onclick中定义 - Uncaught ReferenceError: (function) not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:在HTMLInputElement.onclick中未定义函数 - Uncaught ReferenceError: function is not defined at HTMLInputElement.onclick Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick - Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM