Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 无法将输入字段值传递给角度2中的服务_Angular_Angular Services - Fatal编程技术网

Angular 无法将输入字段值传递给角度2中的服务

Angular 无法将输入字段值传递给角度2中的服务,angular,angular-services,Angular,Angular Services,触点-bar.component.ts saveContacts(): void{ var contact = new Contact({}); contact.id = 1; contact.name = (<HTMLInputElement>document.getElementById("contactName")).value; contact.mobile = parseInt((<HTMLInputElement>document.getElementByI

触点-bar.component.ts

saveContacts(): void{
var contact = new Contact({});
contact.id = 1;
contact.name = 
(<HTMLInputElement>document.getElementById("contactName")).value;
contact.mobile = parseInt((<HTMLInputElement>document.getElementById("contactMobile")).value);
this.contactService.saveContact(contact);
}
saveContacts():void{
var触点=新触点({});
contact.id=1;
contact.name=
(document.getElementById(“contactName”)).value;
contact.mobile=parseInt((document.getElementById(“contactMobile”)).value);
此.contactService.saveContact(contact);
}
联系人:http://www.service.ts.cn

saveContact(contact: Contact) {
    var contacts: Array<Contact> = 
JSON.parse(LocalStorage.getItem("contacts"));
    contacts.push(contact);
    localStorage.setItem("contacts", JSON.stringify(contacts));
}
saveContact(联系人:contact){
变量联系人:数组=
parse(LocalStorage.getItem(“contacts”);
触点。推(触点);
setItem(“contacts”,JSON.stringify(contacts));
}
错误:无法读取null的属性“push”


首先,关于您的错误:您收到的
无法读取null的属性“push”
,因为您试图获取不存在的联系人数据(通过localstoarge)。这就是为什么在将数据推入阵列时,会提示您该错误

但是,代码中有一些问题需要解决

  • 删除此行: var contacts:Array=JSON.parse(LocalStorage.getItem(“contacts”)

  • 不要将数据作为数组发送,请尝试以下操作:

  • var contact =  {
        contact.id : 1;   
        contact.name:(<HTMLInputElement>document.getElementById("contactName")).value;  
        contact.mobile: parseInt((<HTMLInputElement>document.getElementById("contactMobile")).value);
        }
    
    this.contactService.saveContact(contact);
    
    **contact.service.ts**
    
    saveContact(contact: any) {
        localStorage.setItem("contacts", JSON.stringify(contact));
    }
    
    代码:

    var contact =  {
        contact.id : 1;   
        contact.name:(<HTMLInputElement>document.getElementById("contactName")).value;  
        contact.mobile: parseInt((<HTMLInputElement>document.getElementById("contactMobile")).value);
        }
    
    this.contactService.saveContact(contact);
    
    **contact.service.ts**
    
    saveContact(contact: any) {
        localStorage.setItem("contacts", JSON.stringify(contact));
    }
    
    var触点={
    联系人:1;
    contact.name:(document.getElementById(“contactName”)).value;
    contact.mobile:parseInt((document.getElementById(“contactMobile”)).value);
    }
    此.contactService.saveContact(contact);
    **联系人:http://www.service.ts.cn**
    保存联系人(联系人:任意){
    setItem(“contacts”,JSON.stringify(contact));
    }
    
    这与Angular2有什么关系?显然
    JSON.parse(LocalStorage.getItem(“contacts”)
    不返回数组,但
    null