Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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
Javascript 意外标记。需要构造函数、方法、访问器或属性_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 意外标记。需要构造函数、方法、访问器或属性

Javascript 意外标记。需要构造函数、方法、访问器或属性,javascript,angular,typescript,Javascript,Angular,Typescript,我试图将对象推入数组,但它显示了错误 意外标记。应为构造函数、方法、访问器或属性 在Model-form.component.ts文件中,我创建了一个伪json数组,它是test json数组: test: any[] = [{ "cat_id": "1", "cat_name": "One", "cat_type": "One", "displayOrder": 1, "columns": [{

我试图将对象推入数组,但它显示了错误

意外标记。应为构造函数、方法、访问器或属性

在Model-form.component.ts文件中,我创建了一个伪json数组,它是test

json数组:

test: any[] = [{
        "cat_id": "1",
        "cat_name": "One",
        "cat_type": "One",
        "displayOrder": 1,
        "columns": [{
            "category": "One",
            "name": "one"
        }]
    },
    {
        "cat_id": "2",
        "cat_name": "SECURITY",
        "cat_type": "security",
        "displayOrder": 2,
        "columns": [{
            "category": "Two",
            "name": "two"
        }]
    },
    {
        "cat_id": "3",
        "cat_name": "COLLOBORATION",
        "cat_type": "colloboration",
        "displayOrder": 3,
        "columns": [{
            "category": "Three",
            "name": "three"
        }]
    },
    {
        "cat_id": "4",
        "cat_name": "NEW ARCHITECTURE",
        "cat_type": "newarch",
        "displayOrder": 4,
        "columns": [{
            "category": "Four",
            "name": "four"
        }]
    }
];
我尝试将对象推入下面代码给出的测试数组中

this.test.push({
    'cat_name': 'fdas',
    'cat_type': 'fsda'
});

但它显示出错误。类型脚本不接受类内的顶级表达式

请走开

this.test.push({
    'cat_name': 'fdas',
    'cat_type': 'fsda'
});
在函数内部运行函数。

请看以下内容:

export class ModelFormComponent implements OnInit {
 langs: string[] = [
    'English',
    'French',
    'German',
    'Chinese',
    'Vietnamese',
  ];

test: any[] = [
{
"cat_id": "1",
"cat_name": "One",
"cat_type": "One",
"displayOrder":1,
"columns": [
{
"category": "One",
"name": "one"       
}
]
},
{
"cat_id": "2",
"cat_name": "SECURITY",
"cat_type": "security",
"displayOrder":2,
"columns": [
{
"category": "Two",
"name": "two"
}
]
},
{
"cat_id": "3",
"cat_name": "COLLOBORATION",
"cat_type": "colloboration",
"displayOrder":3,
"columns": [
{
"category": "Three",
"name": "three"
}
]
},
{
"cat_id": "4",
"cat_name": "NEW ARCHITECTURE",
"cat_type": "newarch",
"displayOrder":4,
"columns": [
{
"category": "Four",
"name": "four"
}
]
}
];




this.test.push({
    'cat_name': 'fdas',
    'cat_type': 'fsda'
});
在Angular 5中,您不能推送到类内部的数组,类仅仅是属性的表示

要使其发挥作用,您必须做出以下选择:

1:在组件范围内的方法中使用它:

ngOnInit() {
    this.myform = new FormGroup({
      name: new FormGroup({
        firstName: new FormControl('', Validators.required),
        lastName: new FormControl('', Validators.required),
      }),
      email: new FormControl('', [
        Validators.required,
        Validators.pattern("[^ @]*@[^ @]*")
      ]),
      password: new FormControl('', [
        Validators.required,
        Validators.minLength(8)
      ]),
      language: new FormControl()
    });
        this.test.push({
    'cat_name': 'fdas',
    'cat_type': 'fsda'
});
}

2:将其用作导入的类,并将项推入数组:

    import { Component, OnInit,NgModule, Pipe } from '@angular/core';
    import { myClass } from './models/myClass';
    import {ReactiveFormsModule,
        FormsModule,
        FormGroup,
        FormControl,
        Validators,
        FormBuilder} from '@angular/forms';

    @Component({
      selector: 'model-form',
      templateUrl: './model-form.component.html',
      styleUrls: ['./model-form.component.css']
    })
    export class ModelFormComponent implements OnInit {
   constructor(private MyClass: myClass) {
}

  ngOnInit() { 
    this.MyClass.test.push({
    'cat_name': 'fdas',
    'cat_type': 'fsda'
});
}

是的,Praveen,当我从类更改为内部函数时,它正在工作。它像一个charmi一样工作,需要另一个澄清,是否可以在cat_id:4的位置更新json数组。请帮帮我praveen@ramu,很高兴看到它正在工作,您可以尝试这样的方法,var checkId=this.test.findIndexel=>el.cat_id==4;其中checkId是此.test数组中对象的索引,其中cat_id==4现在可以使用checkId、this.test[checkId].push或this.test[checkId]={//在此处更新对象}更新代码。