小编Igo*_*ets的帖子

如何使用一个组件来验证其他两个组件?

我有三个组件:GalleryAddComponent添加一个新元素,GalleryItemComponent编辑元素,FieldsComponent我想在组件中使用的表单:GalleryAddComponent和GalleryItemComponent.所有组件都在里面GalleryComponent.但是当我去组件GalleryAddComponent添加一个新元素时,我得到错误:ERROR TypeError: Cannot read property 'controls' of undefined.也在组件中:GalleryItemComponent.

帮助解决此问题,以便编辑和添加逻辑正常工作.

GalleryAddComponent的模板

<div class="card">
    <div class="card-body">
        <form [formGroup]="angForm" novalidate>
                <app-fields [formGroup]="angForm"></app-fields>
            <div class="form-group but-group">
                <button (click)="addPost(title.value, url.value);  angForm.reset(title.value, url.value)"
                        [disabled]="angForm.pristine || angForm.invalid"
                        class="btn btn-primary">Add
                </button>
                <a routerLink="/" class="btn btn-danger">Back</a>
            </div>
        </form>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

GalleryAddComponent的代码

export class GalleryAddComponent implements OnInit {
    angForm: FormGroup;
    isAdded: boolean = false;
    constructor(private fb: FormBuilder, private galleryService: GalleryService) {}

    ngOnInit() …
Run Code Online (Sandbox Code Playgroud)

html javascript validation typescript angular

7
推荐指数
1
解决办法
288
查看次数

如何在闭包中乘以数字

如何实现用于乘以任意数量的数字的锁定功能。

调用示例:

multiply(1)(2)(3)(4)(5) // 120
Run Code Online (Sandbox Code Playgroud)

为了完成这个任务,需要重新定义toString内部函数的方法,该方法应该返回累积结果,但我有结果NaN。

multiply(1)(2)(3)(4)(5) // 120
Run Code Online (Sandbox Code Playgroud)

javascript closures

4
推荐指数
1
解决办法
1330
查看次数

如何使方法"toString"私有?

"武器"如何实现方法"toString",这对于Object.keys()是不可用的.

function Weapon(name,damage) {
    this.name = name;
    this.damage = damage;
    this.toString = function () {
        return this.name + this.damage + " damage";
    }
}
var bow = new Weapon(' Golden bow, ',20);
var sword = new Weapon(' Magic sword, ', 40);
console.log(bow.toString());
console.log(sword.toString());
Object.defineProperty(Weapon, "toString", {enumerable: false});
Run Code Online (Sandbox Code Playgroud)

javascript

4
推荐指数
1
解决办法
113
查看次数

为什么按价格排序不起作用?

在该sortProductsByPrice (sortOrder)方法中,当我删除或添加新产品时,排序不起作用,排序仅适用于this.products默认情况下阵列中的产品.

(产品按点击排序<th> Price: </ th>).

在移除/添加货物之后,分类也必须使用.

//Product Creation Class
class Product {
    constructor(name, count, price) {
        this.name = name;
        this.count = count;
        this.price = price;
    }
}
Product.SORT_ORDER_ASC = 1;
Product.SORT_ORDER_DESC = -1;
// ?lass where products are recorded
class Shop {
    constructor() {
        this.products = [];
    }

    //method for adding a product
    addProduct(newProduct) {
        this.products.push(newProduct);
    }

    //method for remove product by name
    deleteProductByName(productName) {
        let i = this.products.length;
        while (i--) {
            if …
Run Code Online (Sandbox Code Playgroud)

javascript arrays sorting dom ecmascript-6

3
推荐指数
1
解决办法
115
查看次数

如何在 MatSnackBar 中使用垫子图标?

我想在显示来自使用MatSnackBar. (材料模块在应用程序的模块中导入)。

但是,默认的小吃店不支持图标。我如何实现这样的功能?

import { Injectable } from '@angular/core'
import {MatSnackBar} from '@angular/material/snack-bar';
import {MatSnackBarHorizontalPosition, MatSnackBarVerticalPosition} from "@angular/material";


@Injectable()

export class MessagesService {
    horizontalPosition: MatSnackBarHorizontalPosition = 'right';
    verticalPosition: MatSnackBarVerticalPosition = 'top';

    constructor(private snackBar: MatSnackBar) {
    }

    info(message: string) {
        const icon = `<mat-icon>info</mat-icon>`;
        const msg = `${icon} ${message}`;
        this.snackBar.open(msg, 'X', {
            duration: 2000,
            horizontalPosition: this.horizontalPosition,
            verticalPosition: this.verticalPosition,
            panelClass: ['info-message']
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

typescript angular-material angular

3
推荐指数
1
解决办法
4935
查看次数

如何将字符串解析为比较运算符?

我实现了一种根据指定选项过滤商品的方法。该方法以包含搜索参数的选项对象作为参数,例如:{name: "item 2", Price: "<= 1000", count: "> 2"},每个选项都是可选的。该方法必须返回带有商品的过滤数组。filterProductBy(选项)。\n我设法按名称过滤。告诉我如何正确地按数字过滤,以便过滤器调用如下所示:

\n\n
console.log(shop.filterProductBy({\nname: "product 1",\ncount: ">1",\nprice: "<=1000"}));\n
Run Code Online (Sandbox Code Playgroud)\n\n

“计数”和“价格”需要与比较运算符一起解析,比较运算符设置过滤器的范围,这里有一个可以做到这一点的函数,我如何在我的方法中实现这个函数,一切都会工作?

\n\n

\r\n
\r\n
console.log(shop.filterProductBy({\nname: "product 1",\ncount: ">1",\nprice: "<=1000"}));\n
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

所有代码:

\n\n

\r\n
\r\n
function parseCompOperator(oString, lhs) {\r\n    if (oString.match("(?:<|>)=?\\\\d+") === null) {\r\n        return "Invalid input";\r\n    }\r\n\r\n    let rator = oString.match("(?:<|>)=?")[0];\r\n    let rhs = oString.match("\\\\d+")[0] * 1;\r\n\r\n    if (rator === \'>\') {\r\n        return (lhs > rhs);\r\n    }\r\n    else if (rator === \'<\') {\r\n        return (lhs < rhs);\r\n    }\r\n    else if …
Run Code Online (Sandbox Code Playgroud)

javascript

2
推荐指数
1
解决办法
602
查看次数